文章已过期
iphone手机浏览器或者默认的UIWebView和WKWebView都可以展示pdf,但是如果pdf上有电子签章啥的,他们都展示不了了。
@interface ViewController () <QLPreviewControllerDataSource>
@property (strong, nonatomic)QLPreviewController *previewController;
@property (copy, nonatomic)NSURL *fileURL; //文件路径
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.previewController = [[QLPreviewController alloc] init];
self.previewController.dataSource = self;
}
//预览网络文件
- (void)previewInternet:(id)sender {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSString *urlStr = @"pdf下载链接";
NSString *fileName = [urlStr lastPathComponent]; //获取文件名称
if (![fileName containsString:@".pdf"]) {
fileName = [fileName stringByAppendingString:@".pdf"];
}
NSURL *URL = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//判断是否存在
if([self isFileExist:fileName]) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
self.fileURL = url;
[self addChildViewController:self.previewController];
self.previewController.view.frame = self.view.frame;
[self.view addSubview:previewController.view];
//刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
[self.previewController refreshCurrentPreviewItem];
}else {
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
return url;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
self.fileURL = filePath;
[self addChildViewController:self.previewController];
self.previewController.view.frame = self.view.frame;
[self.view addSubview:previewController.view];
//刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
[self.previewController refreshCurrentPreviewItem];
}];
[downloadTask resume];
}
}
//判断文件是否已经在沙盒中存在
-(BOOL) isFileExist:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL result = [fileManager fileExistsAtPath:filePath];
return result;
}
#pragma mark - QLPreviewControllerDataSource
- (id<QLPreviewItem>)previewController:(QLPreviewController*)controller previewItemAtIndex:(NSInteger)index
{
QLPreviewItemCustom * previewItem = [QLPreviewItemCustom new];
previewItem.previewItemTitle = @"pdf电子签章展示";
previewItem.previewItemURL = self.fileURL;
return previewItem;
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController{
return 1;
}
@end
#import <UIKit/UIKit.h>
#import <QuickLook/QuickLook.h>
@interface ViewController : UIViewController
@end
@interface QLPreviewItemCustom : NSObject <QLPreviewItem>
@property (nonatomic,readwrite) NSURL * previewItemURL;
@property (nonatomic,readwrite) NSString * previewItemTitle;
@end
@implementation QLPreviewItemCustom
@end