不知道大家有沒有遇到過這樣的需求,用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的地址也是可以打開的。