打开本地或网络PDF word excel PPT等文件(提供多种方式)

以前的项目中有要求预览PDF文件,有本地的和网络的,所以就学习了下。打开PDF word excel PPT的文件一般有两种方式,QuickLook框架和UIDocumentInteractionController类,这篇文章主要介绍着两种打开方式,以及本地打开和网络打开两种方式。堵上demo下载链接:https://github.com/ZhengYaWei1992/ZWOpenFileDemo

先看一下UIDocumentInteractionController的使用。

self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
        self.documentInteractionController.delegate = self;
        [self.documentInteractionController presentPreviewAnimated:YES];

UIDocumentInteractionControllerDelegate的代理方法。

#pragma mark -UIDocumentInteractionControllerDelegate代理方法
//这是必须实现的方法,否则不会显示
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}
//设置预览名称
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller{
    controller.name = @"附件预览";
}
//点击Done结束预览
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller{
    
}

再看一下QuickLook框架中如何打开本地文件。使用的时候要导入这个框架:#import

#import 

@interface QuickLookViewController ()
@property (nonatomic,strong) QLPreviewController *previewController;
@end

@implementation QuickLookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"附件预览";
    _previewController = [[QLPreviewController alloc] init];
    _previewController.dataSource = self;
    _previewController.delegate = self;
    
    _previewController.view.frame = CGRectMake(0, 64, self.view.frame.size.width , self.view.frame.size.height);
    //_previewController.currentPreviewItemIndex = 2;
    [self addChildViewController:_previewController];
    [self.view addSubview:_previewController.view];
    [_previewController reloadData];
}

#pragma mark -代理方法
- (id ) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index{
    return self.fileURL;
}
//必须实现
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}

最后在看一下如何基于webView去下载,如果下载成功就基于本地打开,自动缓存PDF word excel PPT等文件。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _openFileWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 0)];
    _openFileWebView.delegate = self;
    _openFileWebView.backgroundColor = [UIColor cyanColor];
    [_openFileWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.fileURLString]]];
}

webView代理方法。

#pragma mark - webView代理
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"网络加载");
    NSURL *targetURL = [NSURL URLWithString:self.fileURLString];
    NSString *docPath = [self documentsDirectoryPath];
    NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docPath, [targetURL lastPathComponent]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //是否已经下载
    BOOL hasDownLoad= [fileManager fileExistsAtPath:pathToDownloadTo];
    if (hasDownLoad) {
        NSLog(@"下载过了");
        self.fileURL = [NSURL fileURLWithPath:pathToDownloadTo];
        _qlVC = [[QLPreviewController alloc]init];
        _qlVC.delegate = self;
        _qlVC.dataSource = self;
        [self presentViewController:_qlVC animated:NO completion:nil];
    }else {
        NSLog(@"没下载过");
        NSURL *targetURL = [NSURL URLWithString:self.fileURLString];
        NSData *fileData = [[NSData alloc] initWithContentsOfURL:targetURL];
       
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        [fileData writeToFile:[NSString stringWithFormat:@"%@/%@", documentsDirectory, [targetURL lastPathComponent]] atomically:YES];
        NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
        [_openFileWebView loadRequest:request];
    }
}

QLPreviewController代理。

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}
- (id )previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return self.fileURL;
}

你可能感兴趣的:(打开本地或网络PDF word excel PPT等文件(提供多种方式))