iOS 选择本地附件,支持添加,删除,预览文件!

近段时间做了下选择文件的功能,可选取word、pdf、txt等文档类型,文件可添加,删除,并在线预览。在做之前看了下微信的选择文件,是利用iPhone系统自带的"文件"APP来实现的,本身由于iOS系统的原因,不像安卓可以自己建立文件夹访问,因此我也用微信从打开“文件”APP里面来选择文件:

Demo地址!!!

下面是实现后的效果图:


预览图.gif

下面说下主要实现步骤:
打开系统"文件"app的类是:UIDocumentInteractionController

//遵循相关代理
@interface ViewController ()<UIDocumentPickerDelegate, UIDocumentInteractionControllerDelegate>

打开文件app:

//选择文件,打开文件app
-(void)openFileAndSlectFile{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    
    UIAlertAction *fileAction = [UIAlertAction actionWithTitle:@"选择文件" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        //制定文件类型
        NSArray *documentTypes = @[@"public.content", @"public.text", @"public.source-code ", @"public.image", @"public.audiovisual-content", @"com.adobe.pdf", @"com.apple.keynote.key", @"com.microsoft.word.doc", @"com.microsoft.excel.xls", @"com.microsoft.powerpoint.ppt"];
        
        UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
        documentPickerViewController.delegate = self;
        documentPickerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:documentPickerViewController animated:YES completion:nil];
        
    }];
    
    [fileAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
    [alertVC addAction:fileAction];
    
  
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [self presentViewController:alertVC animated:true completion:nil];
    
}

选择文件后的代理方法:

#pragma mark - 选择文件代理方法
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    
    NSArray *array = [[url absoluteString] componentsSeparatedByString:@"/"];
    NSString *fileName = [array lastObject];
    fileName = [fileName stringByRemovingPercentEncoding];
    NSLog(@"选择的文件名称=== %@",fileName);
    
    BOOL fileUrlAuthozied = [url startAccessingSecurityScopedResource];
    if(fileUrlAuthozied){
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError *error;
        // __block NSData *fileData;
        [fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
            //保存文件地址
            [self.selectArray addObjectsFromArray:@[newURL]];
            //保存文件名称
            [self.saveNameArray addObjectsFromArray:@[fileName]];
            
            [self.collectionView reloadData];
        }];
        [url stopAccessingSecurityScopedResource];
        
    }else{
        //Error handling
    }
}

预览文件:

#pragma mark ======= 预览附件 =======
            //预览文件
             UIDocumentInteractionController *documentVC= [UIDocumentInteractionController interactionControllerWithURL:self.selectArray[indexPath.row]];
              documentVC.delegate=self;
                
            if ([documentVC presentPreviewAnimated:YES])
                {
                    NSLog(@"打开成功");
                }
            else
                {
                    NSLog(@"打开失败");
                }

打开文件相关代理方法:

#pragma mark =======  打开文件相关代理方法 ======
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
       return self;
}

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
       return self.view;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
        return self.view.frame;
}

删除添加的逻辑这里就不写出来了,我是用 UICollectionViewCell 做的,大家有需要下载Demo查看即可!

以上就是本地文件的选择以及预览的功能,如果是网上的附件预览,直接用WKWebView加载,就可实现在线预览!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 导语 环信官方IOS版Demo功能很强大,却没有实现【发送文件】的功能。但是我们在实际项目开发中,用户之间经常需要...
    江南孤鹜阅读 5,726评论 9 19
  • 需求一注册App为可共享应用程序 1、 在info.plist中注册本App为可共享应用程序以及注册可接受文件类型...
    Eugene_iOS阅读 3,990评论 0 3
  • 前些日子看见小姑家的狗,觉得甚是可爱,今天就画一只吧,有你有狗有阳光,就是我想要的将来
    最小号阅读 823评论 0 0
  • 第三期的《忘不了餐厅》录制中,小敏爷爷的好朋友兼菜场同事前来探望他。导演组并未告知小敏爷爷他的好朋友会来,所以当...
    最初之前阅读 181评论 0 1
  • 别去治了随它吧几年总是能撑过的吧 别害怕 好想去算一下命呀:)
    何不去阅读 319评论 0 0