iOS 文件下载保存预览功能实现

一、文件下载

//urlString:文件资源路径

- (void)downloadFileWithUrl:(NSString *)urlString

success:(ApiSuccess)success

failure:(ApiFailure)failure

{

//获取本地路径:即文件下载后需要存储的路径

NSString * directory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

NSString * fileName = [urlString componentsSeparatedByString:@"/"].lastObject;

NSString * loacalPath = [directory stringByAppendingPathComponent:fileName];

//资源路径编码:防止路径中含有中文

NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];

//下载请求

NSURLRequest * request = [NSURLRequest requestWithURL:url];

NSURLSessionDownloadTask * task = [self downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

NSLog(@"下载进度:%lld / %lld", downloadProgress.completedUnitCount, downloadProgress.totalUnitCount);

} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

//设置下载后需要存储的路径,targetPath:下载时存储的临时路径

return [NSURL fileURLWithPath:loacalPath];

} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

NSLog(@"下载后存储的路径:%@",[filePath path]);

if (error) {

failure(@"下载失败");

NSLog(@"requestUrl:%@ error:%@", urlString, error);

}else{

success([filePath path], YES, @"下载成功");

}

}];

[task resume];

//该方法根据一个resumeData来进行断点下载。在下载cancel或者suspend的时候通过cancelByProducingResumeData方法将当前的resumeData值写入本地plist文件,下次下载的时候从本地plist取出resumeData信息,再通过downloadTaskWithResumeData进行下载就可以了。

//self downloadTaskWithResumeData:<#(nonnull NSData *)#> progress:<#^(NSProgress * _Nonnull downloadProgress)downloadProgressBlock#> destination:<#^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response)destination#> completionHandler:<#^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error)completionHandler#>

}

参考链接:

1、iOS下载word等文件并预览 - 简书

二、保存到系统文件APP

文件下载后默认是保存到我们设置的沙盒路径,用户是没办法找到该文件的,如果要在系统“文件”APP(也叫“files” app)中看到该文件,有两种方式。

方式1:在info.plist文件中添加两个键值对,第一个是 UIFileSharingEnabled,这个可以使 iTunes 分享你文件夹内的内容;第二个是 LSSupportsOpeningDocumentsInPlace ,它保证了你文件夹内本地文件的获取权限,你需要将这两个键值对的值设置为 YES 。如图:

再运行程序,打开系统中“文件”的app,就可以获取就可以在 Files 里面 我的 iPhone 中看到以你项目名为文件夹名的文件夹(如果你在 Files 中看不到 我的 iPhone这一项,你只需要在照片中选中一张照片共享到 Files 里面,就可以看到这一项了)。

但这种方式会将沙盒内 Documents 文件夹内的所有文件都显示出来,因此对一些私密的文件来说是不安全的,那么应该如何处理这些私密文件呢?

这个取决于它的重要性,如果它不是那么重要的,我们可以将它们存放在 cachesDirectory 或者是 temporaryDirectory 文件夹下面;如果它是重要的文件,大多数情况下,我们是需要将它们备份在 iCloud 上的,这样的文件我们建议将它存放在 applicationSupportDirectory 目录下,这样用户就看不到了。

方式2:使用UIDocumentInteractionController弹出文件控制器,在选择存储到文件,有针对性的存储保存文件到“文件”APP,这样私密文件就不会暴露出去了。

如果不能存储,试试在info.plist中添加“Supports Document Browser”键值,值为YES,我是没有添加直接就可以了。

文件控制器如图:

弹出代码如下:

@interface FileTestViewController ()

//这里一定要将_documentController设为全局变量,否则无法正常使用

@property (nonatomic, strong) UIDocumentInteractionController *documentController;

@end

@implementation FileTestViewController

//保存文件

- (void)clickToSave

{

_documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];

[_documentController presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];

}

@end

参考链接:

【链接】iOS-对接系统自带“File”app即(文件)app的实 https://blog.csdn.net/lyz0925/article/details/104460366

三、文件预览

可以使用UIDocumentInteractionController、QuickLook或者webView打开文件。

1、UIDocumentInteractionController 和 QuickLook 不能在线预览,只能加载本地文件。

2、QLPreviewController可以一起浏览多个文件,而UIDocumentInteractionController一次只能浏览一个文件。

3、使用QLPreviewController时,需要导入QuickLook.framework,并遵守其数据源和代理方法。

参考:iOS 文件预览的四种方法 - 简书

参考阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。