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
- UIDocumentInteractionController 文档交互控制器 pdf 游览
- iOS预览PDF文档 UIDocumentInteractionController
- UIDocumentInteractionController -iOS预览、打开文档(doc、pdf等)
- UIDocumentInteractionController 预览和打开文档
- 游览器---文档流
- UIDocumentInteractionController
- PDF阅读器---UIDocumentInteractionController
- 之利用UIDocumentInteractionController打开和预览文档
- UIDocumentInteractionController之程序间文档共享
- 使用第三方APP打开本地文档 (UIDocumentInteractionController)
- 【iOS功能实现】之利用UIDocumentInteractionController打开和预览文档
- iOS利用UIDocumentInteractionController和Quick Look打开或预览文档
- UIDocumentInteractionController的使用之打开和预览文档
- xib和控制器交互
- 文档交互
- (浏览文档)UIDocumentInteractionController打开自己的第三方应用、使用QLPreviewController浏览文档
- 利用UIDocumentInteractionController文档预览以及调用iPhone里第三方相关的app打开文档
- 在iphone/ipad 上预览阅读服务器端的 PDF not UIDocumentInteractionController
- JAVA性能分析之使用火焰图
- android观察者模式
- CSS:两栏布局,三栏布局
- Iterator ------迭代器
- android AP热点(wifi热点)开发
- UIDocumentInteractionController 文档交互控制器 pdf 游览
- 使用夜神模拟器调试HBuilder Android项目
- C# 单元测试EntityFramework中的DbFunctions中的函数
- VR系列——Oculus Publishing文档:七、常见问题
- C语言中的操作符优先级
- 剑指:反转链表
- 给校园招聘中应聘嵌入式程序员的几点建议
- socket通信 简单的客户端和服务器的通信 服务器的代码
- 前端模块化方案的比较(webpack/require.js/seajs/browserify