項(xiàng)目中要顯示PDF協(xié)議,文件里面有電子簽章讼庇,原本用的WKWebView直接把PDF文件的URL扔過去就好了,覺得很方便近尚,后來才發(fā)現(xiàn)蠕啄,紅紅的印章沒出來。在網(wǎng)上找了很多資料戈锻,最終選擇QLPreviewController歼跟,實(shí)現(xiàn)分為兩步:
1.下載PDF文件保存在本地
2.用QLPreviewController打開
主要代碼:
-(void)loadPdfResource:(NSString *)url{
NSURL *Url = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
pdfFilePath = [self getFullPathWithLastPathComponent:Url.lastPathComponent];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:pdfFilePath])
{
// 下載pdf數(shù)據(jù)
// [SVProgressHUD showWithStatus:@"loading..."];
self.pdfData=[[NSMutableData alloc]init];
NSURLSessionConfiguration *config =[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *task = [session dataTaskWithURL:Url];
[task resume];
}else{
// NSLog(@"文件存在");
[self pushQLPreviewController];
}
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler{
//允許繼續(xù)響應(yīng)
completionHandler(NSURLSessionResponseAllow);
//獲取文件的總大小
// NSInteger totalLength = response.expectedContentLength;
}
#pragma mark --- 接收到數(shù)據(jù)調(diào)用
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data
{
//將每次接受到的數(shù)據(jù)拼接起來
[self.pdfData appendData:data];
//計(jì)算當(dāng)前下載的長度
// NSInteger nowlength = self.pdfData .length;
// CGFloat value = nowlength1.0/self.totalLength;
}
#pragma mark ---下載完成調(diào)用
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(nullable NSError *)error{
NSLog(@"self.pdfData:\n%@",self.pdfData);
// NSString *filename =[self getFullPath];
[self.pdfData writeToFile:pdfFilePath atomically:YES];
// [SVProgressHUD dismiss];
if (self.pdfData) {
NSLog(@"OK");
[self showPDFWebView:pdfFilePath];
}else{
NSLog(@"Sorry");
}
}
#pragma mark --- 返回加載文件個(gè)數(shù)
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
#pragma mark --- 返回加載路徑
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
return [NSURL fileURLWithPath:pdfFilePath];
}
-(void)showPDFWebView:(NSString *)filename{
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:pdfFilePath])
{
NSLog(@"文件不存在");
}else{
NSLog(@"文件存在");
[self pushQLPreviewController];
}
}
-(void)pushQLPreviewController{
UINavigationController *root = (UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
[root pushViewController:self.QLPVC animated:YES];
}
- (NSString *)getFullPathWithLastPathComponent:(NSString *)lastPathComponent
{
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
path = [NSString stringWithFormat:@"%@/%@",path,@"借款協(xié)議.pdf"];
NSLog(@"filePath:%@",path);
return path;
}
歡迎指正,Demo地址gitHub