打開(kāi)本地或網(wǎng)絡(luò)PDF word excel PPT等文件(提供多種方式)

以前的項(xiàng)目中有要求預(yù)覽PDF文件,有本地的和網(wǎng)絡(luò)的,所以就學(xué)習(xí)了下。打開(kāi)PDF word excel PPT的文件一般有兩種方式,QuickLook框架和UIDocumentInteractionController類,這篇文章主要介紹著兩種打開(kāi)方式,以及本地打開(kāi)和網(wǎng)絡(luò)打開(kāi)兩種方式。堵上demo下載鏈接:https://github.com/ZhengYaWei1992/ZWOpenFileDemo

先看一下UIDocumentInteractionController的使用。

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

UIDocumentInteractionControllerDelegate的代理方法。

#pragma mark -UIDocumentInteractionControllerDelegate代理方法
//這是必須實(shí)現(xiàn)的方法,否則不會(huì)顯示
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}
//設(shè)置預(yù)覽名稱
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller{
    controller.name = @"附件預(yù)覽";
}
//點(diǎn)擊Done結(jié)束預(yù)覽
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller{
    
}

再看一下QuickLook框架中如何打開(kāi)本地文件。使用的時(shí)候要導(dǎo)入這個(gè)框架:#import <QuickLook/QuickLook.h>。

#import <QuickLook/QuickLook.h>

@interface QuickLookViewController ()<QLPreviewControllerDataSource,
QLPreviewControllerDelegate>
@property (nonatomic,strong) QLPreviewController *previewController;
@end

@implementation QuickLookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"附件預(yù)覽";
    _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 <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index{
    return self.fileURL;
}
//必須實(shí)現(xiàn)
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}

最后在看一下如何基于webView去下載,如果下載成功就基于本地打開(kāi),自動(dòng)緩存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(@"網(wǎng)絡(luò)加載");
    NSURL *targetURL = [NSURL URLWithString:self.fileURLString];
    NSString *docPath = [self documentsDirectoryPath];
    NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docPath, [targetURL lastPathComponent]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //是否已經(jīng)下載
    BOOL hasDownLoad= [fileManager fileExistsAtPath:pathToDownloadTo];
    if (hasDownLoad) {
        NSLog(@"下載過(guò)了");
        self.fileURL = [NSURL fileURLWithPath:pathToDownloadTo];
        _qlVC = [[QLPreviewController alloc]init];
        _qlVC.delegate = self;
        _qlVC.dataSource = self;
        [self presentViewController:_qlVC animated:NO completion:nil];
    }else {
        NSLog(@"沒(méi)下載過(guò)");
        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 <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return self.fileURL;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,355評(píng)論 25 708
  • iOS開(kāi)發(fā)系列--網(wǎng)絡(luò)開(kāi)發(fā) 概覽 大部分應(yīng)用程序都或多或少會(huì)牽扯到網(wǎng)絡(luò)開(kāi)發(fā),例如說(shuō)新浪微博、微信等,這些應(yīng)用本身可...
    lichengjin閱讀 4,060評(píng)論 2 7
  • 一.實(shí)現(xiàn)文件下載 本人實(shí)現(xiàn)文件下載使用的是AFNetworking,具體實(shí)現(xiàn)代碼如下: /**下載文件@param...
    々莫等閑々閱讀 4,398評(píng)論 0 2
  • 項(xiàng)目需要在客戶端顯示用戶上傳的多格式文件,包括常見(jiàn)的PDF,Word,Excel。在網(wǎng)上簡(jiǎn)單查看了一下,學(xué)習(xí)了一些...
    37b32340bcbc閱讀 4,938評(píng)論 0 3
  • 那時(shí),樓下,她呼喊著我的名字 我裝作沒(méi)聽(tīng)到 忽視著 沉溺在自己的世界 任她的聲音 回蕩在空氣中 聽(tīng),她又在叫我了 ...
    蒹蒼蒼閱讀 265評(píng)論 0 0

友情鏈接更多精彩內(nèi)容