iOS 使用QLPreviewController预览本地和网络文件

*近在项目中要做一个文档预览的功能,做的时候用到了iOS原生的QLPreviewController类,在此做个记录分享

首先引入头文件

#import <QuickLook/QuickLook.h>

遵循代理

 

QLPreviewControllerDataSource

声明一个QLPreviewController变量

 

@property (strong, nonatomic)QLPreviewController *previewController;

 

再声明一个NSUrl对象存放要返回的文件路径

 

@property (copy, nonatomic)NSURL *fileURL;
 

在viewDidLoad中初始化

  1. – (void)viewDidLoad {
  2. [super viewDidLoad];
  3. self.previewController = [[QLPreviewController alloc] init];
  4. self.previewController.dataSource = self;
  5. }

 

 

在预览本地文件的事件里面写下如下代码

  1. //获取本地文件路径
  2. self.fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@”李碧华佳句赏析.doc” ofType:nil]];
  3. [self presentViewController:self.previewController animated:YES completion:nil];

遵循代理方法

  1. #pragma mark – QLPreviewControllerDataSource
  2. -(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
  3. return self.fileURL;
  4. }
  5. – (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController{
  6. return 1;
  7. }

这样本地文件的预览就成了

 

添加文件到项目的时候要注意,将文件放到工程项目之后,还要到Build Phases的Copy Bundle Resources中将文件添加到资源库,不然使用的时候会找不到文件而崩溃

 

接下来是预览网络文件

在网络文件预览的事件里面写下如下代码(需要引入af)

  1. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  3. NSString *urlStr = @”https://www.tutorialspoint.com/ios/ios_tutorial.pdf”;
  4. NSString *fileName = [urlStr lastPathComponent]; //获取文件名称
  5. NSURL *URL = [NSURL URLWithString:urlStr];
  6. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  7. //判断是否存在
  8. if([self isFileExist:fileName]) {
  9. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  10. NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
  11. self.fileURL = url;
  12. [self presentViewController:self.previewController animated:YES completion:nil];
  13. }else {
  14. [SVProgressHUD showWithStatus:@”下载中”];
  15. NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){
  16. } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
  17. NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  18. NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
  19. return url;
  20. } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  21. [SVProgressHUD dismiss];
  22. self.fileURL = filePath;
  23. [self presentViewController:self.previewController animated:YES completion:nil];
  24. }];
  25. [downloadTask resume];
  26. }

添加文件是否存在判断方法

  1. //判断文件是否已经在沙盒中存在
  2. -(BOOL) isFileExist:(NSString *)fileName
  3. {
  4. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  5. NSString *path = [paths objectAtIndex:0];
  6. NSString *filePath = [path stringByAppendingPathComponent:fileName];
  7. NSFileManager *fileManager = [NSFileManager defaultManager];
  8. BOOL result = [fileManager fileExistsAtPath:filePath];
  9. return result;
  10. }

 

 

这样网络文件预览就成了