iOS SDK:预览和打开文档
iOS中的沙盒可以让平台更加的安全,这也是沙盒给用户带来的最主要好处。不过由于沙盒的严格限制,导致程序之间共享数据比较麻烦。一般在程序间共享文档可以通过UIDocumentInteractionController(该类经常被开发者忽略)。本文中,我将介绍如何使用这个类在其它程序(已经安装在设备中的程序)中预览和打开文档。
UIDocumentInteractionController在iOS 3.2中就已经存在了,使用起来非常灵活,功能也比较强大。它除了支持同设备上app之间的文档分享外,还可以实现文档的预览、打印、发邮件以及复制。
UIDocumentInteractionController的使用非常简单。首先通过调用它唯一的类方法interactionControllerWithURL:,并传入一个URL(NSURL),来初始化一个实例对象。之后设置一下这个view controller的delegate属性,并实现一些恰当的delegate方法。
注意,UIDocumentInteractionController并不是UIViewController的子类,所以必须通知document interaction controller使用哪个view controller来预览文档。
Step 1: 设置项目
在同设备app之间打开文档非常有用,一个常见的实例是在图片编辑器中打开图片,比如iPhoto。
在 Xcode中创建一个新项目,选择“Single View Application”模板(图1)。命名文档,键入公司标识符,Device选择iPhone,设备下边选择“Use Automatic Reference Counting”,其他两项不选(图2)。然后点击“Next”,保存项目,点击“Creat”按钮。

图1

图2
Step 2: 创建用户界面
这个程序的用户界面包括两个按钮,一个是用于在其他app中预览PDF文档,另一个是用户在其他app中打开PDF文档。创建用户界面之前,在view controller执行文件中为每个按钮赋予一个动作,如下:
1.- (IBAction)previewDocument:(id)sender {
2.}
1.- (IBAction)openDocument:(id)sender {
2.}
选择MTViewController.xib,我们需要从右边view controller视图中拖拽两个UIButton实例(图3)。选择左边的File’s Owner objectobject,打开Connections Inspector,把先前创建的动作和按钮连接起来(图4)。

图3

图4
Step 3:预览文档
现在支持的是PDF文档,你可以使用任何PDF文档,但是在关于这个技巧的源文件中,我已经包含了一个PDF例子,就是苹果的iOS编程指南,也可以在线获得。把文档拖至你的项目中,选中“ Copy items into destination group’s folder (if needed)”这个复选框(图5),最后确保文件已经添加至下边的“Documents target”中。

图5
使用UIDocumentInteractionController类注意事项:1. 你需要保存着document interation controller的实例。2.需要实现UIDocumentInteractionControllerDelegate协议。
首先更新view controller的头文件(如下所示)来告诉compiler,MTViewController类遵照UIDocumentInteractionControllerDelegate协议。
1.#import
2.@interface MTViewController : UIViewController
3.@end
在view controller的实现文件中,添加一个私有属性,类型为UIDocumentInteractionController,并将名称命名为documentInteractionController。这个属性存储着document interaction controller,之后会用着。
看看previewDocument:方法的实现,首先获得文档的URL (NSURL) ,由于文档是app的一部分,因此通过NSBundle类获得文档的(NSURL)非常容易,如下:
1.- (IBAction)previewDocument:(id)sender {
2. NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];
3. if (URL) {
4. // Initialize Document Interaction Controller
5. self.documentInteractionController = [UIDocumentInteractionController
interactionControllerWithURL:URL];
6. // Configure Document Interaction Controller
7. [self.documentInteractionController setDelegate:self];
8. // Preview PDF
9. [self.documentInteractionController presentPreviewAnimated:YES];
10. }
11.}
如果返回了一个有效的URL,我们初始化一个UIDocumentInteractionController的实例,并且通过文档的URL。在documentInteractionController的属性中我们存储了一个document interaction controller的引用。view controller将会充当document interaction controller的delegate
如果现在运行app,你会注意到点击预览按钮时,什么都没有发生。因为这里我们首先要实现一个delegate method。
前边提到,UIDocumentInteractionController类并不是UIViewController的子类,而是继承自NSObject。我们需要通知document interaction controller使用哪个view controller进行文档预览。
UIDocumentInteractionControllerDelegate中有一个名documentInteractionControllerViewControllerForPreview:的delegate方法,该方法请求获得一个用于显示(预览)文档的view controller。
我们希望在main view controller中显示预览,所以可简单的返回self,如下代码所示。它的意思是document interfation controller将使用我们的view controller来预览PDF文档——以modal view的方式显示文档。
1.- (UIViewController *) documentInteractionControllerViewControllerForPreview:
(UIDocumentInteractionController *) controller {
2. return self;
3.
当然你可以简化documentInteractionControllerViewControllerForPreview:的实现以满足你的需要。执行委托方法的同时,你可以首次运行app,试试看这个效果(图6),你可以通过邮件分享这个文档,可以打印或者复制。另外,也可以在支持该文档类型的其他app中打开文档,在图7中点击右边的按钮,看看我说的什么意思。

图6

图7
Step 4: 打开文档
为了实现这一目的我们需要实现openDocument:方法。在previewDocument: 方法中,获取到在程序bundle中一个PDF文件的url,用这个url初始化一个UIDocumentInteractionController。
之后设置一下UIDocumentInteractionController的delegate,在这个UIDocumentInteractionController中调用presentOpenInMenuFromRect:inView:方法显示一个菜单。传入的第一个参数CGRect是button的frame,如下所示:
1.- (IBAction)openDocument:(id)sender {
2. UIButton *button = (UIButton *)sender;
3. NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];
4. if (URL) {
5. // Initialize Document Interaction Controller
6. self.documentInteractionController = [UIDocumentInteractionController
interactionControllerWithURL:URL];
7. // Configure Document Interaction Controller
8. [self.documentInteractionController setDelegate:self];
9. // Present Open In Menu
10. [self.documentInteractionController presentOpenInMenuFromRect:[button frame] inView:self.view animated:YES];
11. }
12.}
为了测试openDocument:方法,在真机上运行实例app非常重要。原因很简单,操作系统要检查安装的app是否支持我们要打开的文件类型(UTI)。如果没有找到支持相应文件类型的app,那么菜单中就不会有打开的提示,而这个不能通过iOS模拟器进行测试。
为了测试这个功能,首先要在真机上安装支持PDF的app,比如Dropbox或者Amazon的Kindle app。

图8
总结
使用UIDocumentInteractionController这个类可以简单地实现app之间文档的预览和打开。建议你去看看这个类的参考文档,特别是UIDocumentInteractionControllerDelegate协议——这里面有许多delegate
方法,当遇到大文档或者复杂的工作流时,这些方法都非常的方便。
源文件:
![]() |
/cms/uploads/soft/130515/4196-130515163Q2.zip |
http://www.cocoachina.com/ios/20130515/6212.html
iOS SDK:预览和打开文档的更多相关文章
- iOS Document Interaction(预览和打开文档) 编程指南
原文:http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteracti ...
- IOS 预览pdf,word文档的集中方式
在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...
- 秒级接入、效果满分的文档预览方案——COS文档预览
一.导语 说起 Microsoft Office 办公三件套,想必大家都不会陌生,社畜日常的工作或者生活中,多多少少遇到过这种情况: 本地创建的文档换一台电脑打开,就出现了字体丢失.排版混乱的情况 ...
- 微信小程序之wx.request:fail错误,真机预览请求无效问题解决,安卓,ios网络预览异常
新版开发者工具增加了https检查功能:可使用此功能直接检查排查ssl协议版本问题: 可能原因:0:后台域名没有配置0.1:域名不支持https1:没有重启工具:2:域名没有备案,或是备案后不足24小 ...
- python爬虫处理在线预览的pdf文档
引言 最近在爬一个网站,然后爬到详情页的时候发现,目标内容是用pdf在线预览的 比如如下网站: https://camelot-py.readthedocs.io/en/master/_static/ ...
- iOS图片预览、放大缩小
思路 图片预览,优先考虑基础控件UIImageView.UIButton 图片预览中可能需设置不同的mode,优先考虑UIImageView typedef NS_ENUM(NSInteger, UI ...
- 【原创】iOS图片预览(支持缩放和移动)
1.传入图片 PreViewController.h: #import <UIKit/UIKit.h> @interface PreViewController : UIViewContr ...
- 在其他app里预览文档
本文转载至 http://www.cocoachina.com/newbie/basic/2013/0515/6212.html iOS中的沙盒可以让平台更加的安全,这也是沙盒给用户带来的最主要好处. ...
- IOS 预览word文档的集中方式
在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...
随机推荐
- vmware虚拟机上linux操作系统进行tty1~tty6切换方法和具体步骤
vmware虚拟机上linux操作系统怎样进行tty1~tty6切换? 现象: Linux的终端机(文字)界面与图形界面间的切换热键为: 进入终端机也就是字符界面(tty1-tty6):[Ctrl] ...
- SGU 201 Non Absorbing DFA (DP)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个自动机,给出所有的转移,其中还有一个 ...
- [学习笔记]js动画实现方法,作用域,闭包
一,js动画基本都是依靠setInterval和setTimeout来实现 1,setInterval是间隔执行,过一段时间执行一次代码 setInterval(function(){},500);即 ...
- AngularJS Directive 学习笔记
指令 Directive 指令要点 大漠老师的教学节点 解析最简单的指令 hello: 匹配模式 restrict 解析最简单的指令 hello: template.tempmlateUrl.$tem ...
- ShineTime 是一个效果非常精致的缩略图相册
ShineTime 是一个效果非常精致的缩略图相册,鼠标悬停到缩略图的时候有很炫的闪光效果,基于 CSS3 实现,另外缩略图也会有立体移动的效果.特别适用于个人摄影作品,公司产品展示等用途,快来来围观 ...
- zoj 1081 (改进的弧长算法)(转)
看到网上除了射线法,很长一段代码之外,看到了一个很简单的算法解决这个问题,特意转了过来 /* 这个算法是源自<计算机图形学基础教程>(孙家广,清华大学出版社),在该书 的48-49页,名字 ...
- slf4j+log4j配置(Maven)
首先配置Maven依赖 <!-- http://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency& ...
- Android 开发笔记“Application 理解”
Android 中Application类用法 1. Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时 ...
- VC++ win32 多线程 一边画圆一边画矩形
// WinThreadTest.cpp : Defines the entry point for the application. // #include "stdafx.h" ...
- TreeView控件例子
XmL文件代码: <?xml version="1.0" encoding="utf-8" ?> <Area> <Province ...
