iOS 打開Doc等文件

不知道大家有沒有遇到過這樣的需求,用App打開office文檔,哈哈,很多人會(huì)說不可以,其實(shí)是可以的,而且有很多種方式。
一共有三種方式:(按照蘋果發(fā)布的時(shí)間來說)

第一種方式:UIDocumentInteractionController
- (void)readDocWithDucument{
    NSString * ducumentLocation = [[NSBundle mainBundle]pathForResource:@"用戶模塊接口" ofType:@"docx"];
    NSURL *url = [NSURL fileURLWithPath:ducumentLocation];
    _documentInteractionController= [UIDocumentInteractionController interactionControllerWithURL:url];
    _documentInteractionController.delegate = self;
//    [_documentInteractionController presentPreviewAnimated:YES];
    [_documentInteractionController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES];
}```
遵循代理方法`<UIDocumentInteractionControllerDelegate>`

pragma mark - UIDocumentInteractionController 代理方法

  • (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
    }
  • (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    return self.view;
    }
  • (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    return self.view.bounds;
    }```
    如果方法寫在- (void)viewDidLoad里面的話會(huì)出現(xiàn)問題找不到當(dāng)前view,所以建議解決辦法是利用下面這種調(diào)用方式
[self performSelectorOnMainThread:@selector(readDocWithDucument) withObject:nil waitUntilDone:NO];

對(duì)于聲明documentInteractionController

@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;

大家可以查看這篇文章http://m.itdecent.cn/p/3f03897cf98a

第二種方式:QLPreviewController

引入QuickLook.framework框架
引入頭文件#import <QuickLook/QuickLook.h>
遵循代理<QLPreviewControllerDataSource>

- (void)quickLook{
    self.view.backgroundColor = [UIColor grayColor];
    QLPreviewController * preVC = [[QLPreviewController alloc]init];
    preVC.dataSource = self;
    [self presentViewController:preVC animated:YES completion:nil];
    
}
#pragma mark - QLPreviewControllerDataSource 代理方法
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    NSString * ducumentLocation = [[NSBundle mainBundle]pathForResource:@"用戶模塊接口" ofType:@"docx"];
    NSURL *url = [NSURL fileURLWithPath:ducumentLocation];
    return  url;
}

同樣會(huì)有加載view的問題:如果方法寫在- (void)viewDidLoad里面的話會(huì)出現(xiàn)問題找不到當(dāng)前view,所以建議解決辦法是利用下面這種調(diào)用方式

[self performSelectorOnMainThread:@selector(readDocWithDucument) withObject:nil waitUntilDone:NO];
第三種方式:UIWebView

遵循代理<UIWebViewDelegate>

- (void)readDocfile{
    NSString * ducumentLocation = [[NSBundle mainBundle]pathForResource:@"用戶模塊接口" ofType:@"docx"];
    NSURL *url = [NSURL fileURLWithPath:ducumentLocation];
    
    UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];
    webView.delegate = self;
    webView.multipleTouchEnabled = YES;
    webView.scalesPageToFit = YES;

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    
    [self.view addSubview:webView];
}

三種方式各有千秋,前兩種方式比較相像,但是都不能在線預(yù)覽,只能下載到本地進(jìn)行查看這個(gè)確實(shí)比較蛋疼,但是蘋果最起碼能讓我們?cè)贏pp內(nèi)查看。
這是項(xiàng)目源碼地址
自定義預(yù)覽控制器,一直沒有好的辦法,如有知道的童鞋,煩請(qǐng)指教下。

前兩天發(fā)現(xiàn)可以用系統(tǒng)自帶的瀏覽器打開在線文件的地址,word的地址也是可以打開的。

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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