通过苹果自带的工具UIDocumentInteractionController实现PDF文件下载、预览、分享。
一、准备
创建documentVC、wkView、url、titleStr;加载PDF
@property (strong, nonatomic) WKWebView *wkView;
@property (nonatomic, strong) NSString *url; //文件下载地址or本地文件地址
@property (nonatomic, strong) NSString *titleStr;//文件标题名称
@property (nonatomic, strong) UIDocumentInteractionController *documentVC;//文件预览(写全局变量,否则操作不成功)
- (void)viewDidLoad {
[super viewDidLoad];
if ([_url rangeOfString:@"http"].location !=NSNotFound) {//是http格式的 在线预览
[self.wkView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];//加载页面
}else {//加载本地pdf文档
NSString *filePath = [[NSBundle mainBundle] pathForResource:_url ofType:@"pdf"];
[self.wkView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
}
}
二、下载PDF文档
通过UIDocumentInteractionController存储到手机、分享到其他APP
// MARK: - 点击导航栏右侧按钮 - 下载PDF文档
- (void)rightItemClickFunc {
if (self.url == nil || self.url.length <= 0) {//非空
return;
}
if ([self.url rangeOfString:@"http"].location !=NSNotFound) {//是http格式的 - 在线预览
[self downloadPDFFromeUrl:self.url fileName:_titleStr];
}else {//本地内置pdf文档
NSString *filePath = [[NSBundle mainBundle] pathForResource:self.url ofType:@"pdf"];
[self openPDFWithPath:filePath];
}
}
/// 下载PDF - 在线预览 (url:文件下载地址 name:文件名)
- (void)downloadPDFFromeUrl:(NSString *)url fileName:(NSString *)name {
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);//下载文件存储路径
NSString *localPath = [[pathArray objectAtIndex:0] stringByAppendingPathComponent:[name stringByAppendingString:@".pdf"]];//不暴露url中的文件名
if ([[NSFileManager defaultManager] fileExistsAtPath:localPath]) {//判断沙盒中是否存在此文件
[self openPDFWithPath:localPath];
return;
}
//不存在 重新下载
NSURL *urlStr = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//
NSURLRequest *request = [NSURLRequest requestWithURL:urlStr];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
LoadingEx *loadingEx = [LoadingEx shareLoading];
[loadingEx show];//显示菊花
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"下载进度:%lld %lld",downloadProgress.completedUnitCount,downloadProgress.totalUnitCount);
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSLog(@"下载文件存储路径:%@",localPath);
return [NSURL fileURLWithPath:localPath];//下载文件存储路径
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
[loadingEx dismiss];
[self openPDFWithPath:localPath];
}];
[task resume];//开始下载 否则不会下载
}
/// 打开文件预览
- (void)openPDFWithPath:(NSString *)localPath {
_documentVC = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:localPath]];
_documentVC.delegate = self;//设置分享代理
_documentVC.UTI = @"pdf";//哪类文件支持第三方打开,不写就代表所有文件 pdf.doc.ppt
_documentVC.name = _titleStr;//名称
BOOL canOpen = [_documentVC presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];//打开预览
if (!canOpen) {
NSLog(@"没有程序可以打开选中的文件");
}
}
参考: