IOS檔案系統,檔案管理器
阿新 • • 發佈:2019-02-18
通過一個demo,我們來一起認識一下ios檔案系統。我們一起寫一個檔案管理器。可以查詢檔案,瀏覽目錄,並且可以識別檔案型別,開啟圖片,音訊,視訊等。
首先獲取路徑下的全部檔案路徑
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = self.path ? self.path:@"/Users/liuzixuan/Desktop/三階段(liu)";
self.title = [path lastPathComponent];
self.filePaths = [NSMutableArray array];
NSArray *fileNames = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:nil];
for (NSString *fileName in fileNames) {
if ([fileName hasPrefix:@"."]) {
continue;
}
NSLog(@"%@",fileName);
NSString *filePath = [path stringByAppendingPathComponent:fileName];
[self .filePaths addObject:filePath];
}
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
實現tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.filePaths.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
NSString *filePath = self.filePaths[indexPath.row];
cell.textLabel.text = [filePath lastPathComponent];
//判斷是否是資料夾
if ([self isDirectoryOfPath:filePath]) {
cell.imageView.image = [UIImage imageNamed:@"directory.png"];
}else{
cell.imageView.image = [UIImage imageNamed:@"other.png"];
}
return cell;
}
判斷是否為資料夾的 function
- (BOOL)isDirectoryOfPath:(NSString *)path{
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir = NO;
//方法返回值判斷的是檔案是否存在
if ([fm fileExistsAtPath:path isDirectory:&isDir]&&isDir) {
return YES;
}
return NO;
}
tableview的點選事件,並且識別檔案型別,實現對應的開啟方式
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *filePath = self.filePaths[indexPath.row];
//判斷是否是資料夾
if ([self isDirectoryOfPath:filePath]) {
FilesTableViewController *vc = [FilesTableViewController new];
vc.path = filePath;
[self.navigationController pushViewController:vc animated:YES];
}else if ([filePath hasSuffix:@"mp4"]){
AVPlayerViewController *vc = [AVPlayerViewController new];
vc.player = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:filePath]];
[vc.player play];
[self.navigationController pushViewController:vc animated:YES];
}else if ([filePath hasSuffix:@"jpg"]||[filePath hasSuffix:@"png"]){
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor whiteColor];
UIImageView *iv = [[UIImageView alloc]initWithFrame:self.view.bounds];
[vc.view addSubview:iv];
iv.image = [UIImage imageWithContentsOfFile:filePath];
[self.navigationController pushViewController:vc animated:YES];
}else if ([filePath hasSuffix:@"mp3"]){
self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];
[self.player play];
}else if([filePath hasSuffix:@"rtf"]||[filePath hasSuffix:@"txt"]||[filePath hasSuffix:@".h"]||[filePath hasSuffix:@".m"]){
UIViewController *vc = [UIViewController new];
vc.view.backgroundColor = [UIColor whiteColor];
UITextView *tv = [[UITextView alloc]initWithFrame:self.view.bounds];
[vc.view addSubview:tv];
tv.text = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[self.navigationController pushViewController:vc animated:YES];
}else{
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提示" message:@"目前版本暫不支援此格式,請期待下一版本!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];
[ac addAction:action1];
[self presentViewController:ac animated:YES completion:nil];
}
}
效果如下圖: