iOS關於PDF電子簽章不顯示的解決方案
之前在掘金上釋出的文章原文連結:https://juejin.im/entry/5a71ad616fb9a01cad7c7b8c
最近公司添加個檢視pdf合同的功能,之前隨便百度了下發現webView載入或者View自己畫都可以顯示,不過最後發現少了一樣東西,沒錯紅紅的簽名公章不見了,安卓瀏覽器開啟都沒問題,分享檔案到手機qq、微信、掌閱等常用app開啟也沒有顯示簽章,在一段痛苦的糾結中反思問題查詢解決方案,然後發現qq開啟下方分享按鈕處有個建立PDF,開啟後發現簽章出來了或者選擇印表機也會顯示簽章,因此也確定了竟然能出來那麼肯定有解決方法,於是開始了爬坑。
隨便說一下網路上的載入PDF的第三方框架也並不好用,最終還是在蘋果開發框架中找到了解決方案
開始上程式碼 1.首先匯入系統
QuickLook.fraework
```框架,怎麼匯入不簡述了
2.引入標頭檔案
#import <QuickLook/QuickLook.h>
QLPreviewControllerDataSource
主要一下2個代理方法
(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{ return 1; }
(id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
return [NSURL fileURLWithPath:pdfFilePath]; }
完整程式碼如下:
url 為pdf 地址連結,訪問地址返回pdf 資料流
-(void)loadPdfResource:(NSString *)url{ pdfFilePath = [self getFullPath]; NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:pdfFilePath])
{
// NSLog(@"檔案不存在"); // 下載pdf資料 [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:[NSURL URLWithString:url]];
[task resume];
}else{
// NSLog(@"檔案存在"); [self pushQLPreviewController]; }
}
下載pdf 資料主要 實現了 NSURLSessionDelegate 代理方法
#pragma mark --- 接收到資料呼叫
(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler{ //允許繼續響應 completionHandler(NSURLSessionResponseAllow); //獲取檔案的總大小 // NSInteger totalLength = response.expectedContentLength; }
#pragma mark --- 接收到資料呼叫
(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData )data{ NSPrintf(@"didReceiveData:%@",data); //將每次接受到的資料拼接起來 [self.pdfData appendData:data]; //計算當前下載的長度 // NSInteger nowlength = self.pdfData .length; // CGFloat value = nowlength1.0/self.totalLength; }
#pragma mark ---下載完成呼叫
(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error{ NSLog(@"self.pdfData:\n%@",self.pdfData);
NSString *filename =[self getFullPath]; [self.pdfData writeToFile:filename atomically:YES]; [SVProgressHUD dismiss];
if (self.pdfData) { NSLog(@"OK"); [self showPDFWebView: filename]; }else{ NSLog(@"Sorry"); }
}
下載完成 寫入本地資料 之後 展示pdf,重點來了!
-(void)showPDFWebView:(NSString *)filename{
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:pdfFilePath])
{
NSLog(@"檔案不存在");
}else{
NSLog(@"檔案存在");
[self pushQLPreviewController];
}
}
推出pdf 頁面
-(void)pushQLPreviewController{ QLPreviewController *QLPVC = [[QLPreviewController alloc] init]; QLPVC.dataSource = self; [self.navigationController pushViewController:QLPVC animated:YES]; }
最後需要自定義 self.pdfData
Xcode升級到10.1以後發現present頁面後PDF檔案會有類似安全區的空檔,改為push頁面解決問題,初步判斷應該是我專案中使用了RTNavationController的問題
over~