如果是WebView傀履,要簽代理<UIWebViewDelegate>
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
? ? NSString *pageUrl = request.URL.absoluteString;
? ? NSString *lastName =[[pageUrl lastPathComponent] lowercaseString];
? ? if ([lastName containsString:@".pdf"])
? ? {
? ? ? ? NSData *data = [NSData dataWithContentsOfURL:request.URL];
? ? ? ? [self.webview loadData:data MIMEType:@"application/pdf" textEncodingName:@"GBK" baseURL:nil];
? ? }
? ? return YES;
}
如果是WKWebView捡多,要簽代理<WKNavigationDelegate>
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
? ? NSString *_webUrlStr = navigationAction.request.URL.absoluteString;
? ? NSString *lastName =[[_webUrlStr lastPathComponent] lowercaseString];
? ? if ([lastName containsString:@".pdf"])
? ? {
? ? ? ? NSData *data = [NSData dataWithContentsOfURL:navigationAction.request.URL];
? ? ? ? [self.webView loadData:data MIMEType:@"application/pdf" characterEncodingName:@"GBK" baseURL:nil];
? ? }
? ? decisionHandler(WKNavigationActionPolicyAllow);
}
+ (void)downLoadPdf:(NSString *)url pdf_id:(NSString *)pdf_id block:(APIFilePath)pdfFilePath {
NSMutableDictionary *mPdf_dic = [NSMutableDictionary dictionaryWithDictionary:[Tool getLocalKey:PDFLOCAL_DIC]];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSLog(@"###%@", targetPath);? //原始文件
NSLog(@"**%@", [response suggestedFilename]);? //文件名
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);//下載成功后本地保存id以及文件路徑
NSString *stringUrl = [filePath absoluteString];
mPdf_dic[pdf_id] = stringUrl;
NSDictionary *pdfDic = [NSDictionary dictionaryWithDictionary:mPdf_dic];
[Tool saveLocalvalue:pdfDic key:PDFLOCAL_DIC];
pdfFilePath(stringUrl);
}];
[downloadTask resume];
}
查看文檔時(shí)判斷本地是否已經(jīng)下載
#pragma mark - read
- (void)read {
//本地已下載pdf文件
NSDictionary *pdfDic = [Tool getLocalKey:PDFLOCAL_DIC];
if (pdfDic == nil) {
[Tool saveLocalvalue:pdfDic key:PDFLOCAL_DIC];
}
//文件路徑
NSString *filePathLocal;
NSString *fileUrl = @"pdf的URL";
NSString *fileKey = @"1471509244417_1471509259460";
NSArray *keys = [pdfDic allKeys];
BOOL isDownload = NO; //是否已經(jīng)下載
for (NSString *key in keys) {
if ([key isEqualToString:fileKey]) {
isDownload = YES;
break;
} else {
isDownload = NO;
}
}
if (isDownload) {
filePathLocal = pdfDic[fileKey];
[self openPdf:filePathLocal];
} else {
[WYAPI downLoadPdf:fileUrl pdf_id:fileKey block:^(NSString *filePath) {
[self openPdf:filePath];
}];
}
}
————————————————
版權(quán)聲明:本文為CSDN博主「螢火蟲(chóng)兒飛」的原創(chuàng)文章撑帖,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明肿孵。
原文鏈接:https://blog.csdn.net/weixin_41886208/article/details/87891758