iOS 选择本地附件,支持添加,删除,预览文件!
2020-02-17 本文已影响0人
宏炜
近段时间做了下选择文件的功能,可选取word、pdf、txt等文档类型,文件可添加,删除,并在线预览。在做之前看了下微信的选择文件,是利用iPhone系统自带的"文件"APP来实现的,本身由于iOS系统的原因,不像安卓可以自己建立文件夹访问,因此我也用微信从打开“文件”APP里面来选择文件:
下面是实现后的效果图:
预览图.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加载,就可实现在线预览!