UIDocumentInteractionController 文档交互控制器 pdf 游览

来源:互联网 发布:linux复制命令 编辑:程序博客网 时间:2024/05/02 02:06

在我们的app中可能会涉及有关文档阅读的知识,会涉及到doc,docx,pdf,xls等格式的文件预览,或者调用第三方app打开文件的功能。如果不是严格要求,使用UIWebView打开是一种最为简单快捷的方式(具体方法很简单,在此就不多做说明)。
查了一下资料,UIDocumentInteractionController是iOS 很早就出来的一个功能。但由于平时很少用到,压根就没有听说过它。而我们忽略的缺是一个功能强大的”文档阅读器”。

首先导入QuickLook.framework支持库
UIDocumentInteractionController *documentInteraction;

//(我是用这种方法声明的,如果你用@propoty 请记住要用retain)因为documentInteraction的实例化不是通过alloc的。
//首先获取文件路径

filePathStr = [[NSBundle mainBundle]pathForResource:@"mypdf" ofType:@"pdf"];

//实例化 传入文件路径

documentInteraction = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePathStr]]; //设置代理 documentInteraction.delegate = self;

前面这些基本步骤是预览和调用第三方都需要的前奏。
下面说一面预览:

//这句话就是调用预览的,UIDocumentInteractionController会利用iOS 内部的文档工具打开该文件,直接在当前视图展示文档内容:

[documentInteraction presentPreviewAnimated:YES];

//必须实现的代理方法 预览窗口以模式窗口的形式显示,因此需要在该方法中返回一个view controller ,作为预览窗口的父窗口。如果你不实现该方法,或者在该方法中返回 nil,或者你返回的 view controller 无法呈现模式窗口,则该预览窗口不会显示。

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{      return self; } 

//可选的2个代理方法 (主要是调整预览视图弹出时候的动画效果,如果不实现,视图从底部推出)

- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller {     return self.view; }
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {      return self.view.frame; }

运行结果如图:
(后期补上)

点击左上角的Done退出预览,回到当前页面。右上角的图片,点击可出现发送邮件,赋值,打印等一些功能。(这个可比web功能强大多了吧)

下面该说调用第三方app打开文档了。与平时app的相互调用不同,使用UIDocumentInteractionController不需要知道第三方app的URL Scheme信息。它会自动检测能打开(准确说是操作)它的app。具体原理是这些app在发布时都是进行URl Scheme的相关设置,意图在于告知其他app我有处理文档的功能。(如何让自己的app能被调用去打开其他app的文档请参考:http://blog.csdn.net/kmyhy/article/details/9064089)上代码:

[documentInteraction presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];

注意事项:txt格式用预览打开会出现乱码的情况,此时需要进行处理

先拓展一下webView处理乱码的方法

- (NSString *)examineTheFilePathStr:(NSString *)str{     NSStringEncoding *useEncodeing = nil;     //带编码头的如utf-8等,这里会识别出来     NSString *body = [NSString stringWithContentsOfFile:str usedEncoding:useEncodeing error:nil];     //识别不到,按GBK编码再解码一次.这里不能先按GB18030解码,否则会出现整个文档无换行bug     if (!body) {         body = [NSString stringWithContentsOfFile:str encoding:0x80000632 error:nil];     }     //还是识别不到,按GB18030编码再解码一次.     if (!body) {         body = [NSString stringWithContentsOfFile:str encoding:0x80000631 error:nil];     }      return body;//有值代表需要转换  为空表示不需要转换 } if(body){         [self.webView loadHTMLString:body baseURL: nil];     }else{         NSURL *filePathUrl = [NSURL fileURLWithPath:self.filePathStr];       //  NSLog(@"%@",self.filePathStr);         NSURLRequest *request = [NSURLRequest requestWithURL:filePathUrl];         [self.webView loadRequest:request];     }

其次UIDocumentInteractionController处理乱码的方法

- (void)transformEncodingFromFilePath:(NSString *)filePath{     //调用上述转码方法获取正常字符串     NSString *body = [self examineTheFilePathStr:filePath];     //转换为二进制     NSData *data = [body dataUsingEncoding:NSUTF16StringEncoding];     //覆盖原来的文件     [data writeToFile:filePath atomically:YES];     //此时在读取该文件,就是正常格式啦 } 

原文链接地址 http://www.ithao123.cn/content-8283052.html

参考网址 :https://yq.aliyun.com/articles/5038

0 0