美文网首页iOS开发经验
iOS 打开Doc等文件

iOS 打开Doc等文件

作者: Smallwolf_JS | 来源:发表于2016-10-19 22:51 被阅读1646次

不知道大家有没有遇到过这样的需求,用App打开office文档,哈哈,很多人会说不可以,其实是可以的,而且有很多种方式。
一共有三种方式:(按照苹果发布的时间来说)

第一种方式: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里面的话会出现问题找不到当前view,所以建议解决办法是利用下面这种调用方式
[self performSelectorOnMainThread:@selector(readDocWithDucument) withObject:nil waitUntilDone:NO];

对于声明documentInteractionController

@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;

大家可以查看这篇文章http://www.jianshu.com/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;
}

同样会有加载view的问题:如果方法写在- (void)viewDidLoad里面的话会出现问题找不到当前view,所以建议解决办法是利用下面这种调用方式

[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];
}

三种方式各有千秋,前两种方式比较相像,但是都不能在线预览,只能下载到本地进行查看这个确实比较蛋疼,但是苹果最起码能让我们在App内查看。
这是项目源码地址
自定义预览控制器,一直没有好的办法,如有知道的童鞋,烦请指教下。

前两天发现可以用系统自带的浏览器打开在线文件的地址,word的地址也是可以打开的。

相关文章

网友评论

    本文标题:iOS 打开Doc等文件

    本文链接:https://www.haomeiwen.com/subject/pbzxuttx.html