IOS文件系统,文件管理器

通过一个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];


    }
}

效果如下图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值