iOS QLPreviewController

//QLPreviewController系统控件 可以读取pdf doc docx xls xlsx ppt pptx txt等格式的本地文件或者网络连接

首先要引入 系统类
#import 


引入代理



@property(nonatomic,strong)QLPreviewController *previewController;
@property(nonatomic,strong)NSURL *fileURL;

创建QLPreviewController
  
    _previewController = [[QLPreviewController alloc] init];
    _previewController.dataSource = self;
    _previewController.delegate   = self;
    _previewController.currentPreviewItemIndex = 0;
    
    [self addChildViewController:_previewController];
    [_previewController didMoveToParentViewController:self];
    [self.view addSubview:_previewController.view];
    _previewController.view.frame = CGRectMake(0,NavBarAndStatusHeight, self.view.frame.size.width, self.view.frame.size.height-NavBarAndStatusHeight);
    [_previewController reloadData];
    
    [self actionQLView];
    
    
    NSString *type = [self.filePath pathExtension];//获取后缀名
    
    
    //获取本地文件路径
    self.fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"1.docx" ofType:nil]];
    [self presentViewController:self.previewController animated:YES completion:nil];
    //刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
    [self.previewController refreshCurrentPreviewItem];



代理
#pragma mark - QLDelegate
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    return self.fileURL;
}

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1;
}

- (void)actionQLView {
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
//    NSString *urlStr = @"https://www.tutorialspoint.com/ios/ios_tutorial.pdf";
    NSString *urlStr = self.filePath;
    NSString *fileName = [urlStr lastPathComponent]; //获取文件名称
    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 presentViewController:self.previewController animated:YES completion:nil];
        //刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的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) {
            /*
             - (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
                               forDomain:(NSString *)domain
             {
                 //Pods里面要改,否则会报错
             */
            self.fileURL = filePath;
//            [self presentViewController:self.previewController animated:YES completion:nil];
            //刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
            [self.previewController refreshCurrentPreviewItem];
        }];
        [downloadTask resume];
    }
}

/**
 判断文件是否已经在沙盒中存在

 @param fileName 文件名
 @return 1:存在 0:不存在
 */
- (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;
}

你可能感兴趣的:(iOS QLPreviewController)