iOS SDK:预览和打开文档

移动开发 iOS
UIDocumentInteractionController在iOS 3.2中就已经存在了,使用起来非常灵活,功能也比较强大。它除了支持同设备上app之间的文档分享外,还可以实现文档的预览、打印、发邮件以及复制。

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”按钮。

Step 2: 创建用户界面

这个程序的用户界面包括两个按钮,一个是用于在其他app中预览PDF文档,另一个是用户在其他app中打开PDF文档。创建用户界面之前,在view controller执行文件中为每个按钮赋予一个动作,如下:

  1. - (IBAction)previewDocument:(id)sender { 

  2. - (IBAction)openDocument:(id)sender { 

  3. }

选择MTViewController.xib,我们需要从右边view controller视图中拖拽两个UIButton实例(图3)。选择左边的File’s Owner objectobject,打开Connections Inspector,把先前创建的动作和按钮连接起来(图4)。 

Step 3:预览文档

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

使用UIDocumentInteractionController类注意事项:

1. 你需要保存着document interation controller的实例。

2.需要实现UIDocumentInteractionControllerDelegate协议。

首先更新view controller的头文件(如下所示)来告诉compiler,MTViewController类遵照UIDocumentInteractionControllerDelegate协议。

  1. #import <UIKit/UIKit.h>

  2. @interface MTViewController : UIViewController <UIDocumentInteractionControllerDelegate> 

  3. @end

在view controller的实现文件中,添加一个私有属性,类型为UIDocumentInteractionController,并将名称命名为documentInteractionController。这个属性存储着document interaction controller, 之后会用着。

别走开下页为您带来打开文档、总结、源文件下载

#p#

看看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  

  6. interactionControllerWithURL:URL]; 

  7. // Configure Document Interaction Controller

  8.         [self.documentInteractionController setDelegate:self]; 

  9. // Preview PDF

  10.         [self.documentInteractionController presentPreviewAnimated:YES]; 

  11.     } 

  12. }

如果返回了一个有效的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:  

  2. (UIDocumentInteractionController *) controller { 

  3. return self;

当然你可以简化documentInteractionControllerViewControllerForPreview:的实现以满足你的需要。执行委托方法的同时,你可以首次运行app,试试看这个效果(图6),你可以通过邮件分享这个文档,可以打印或者复制。另外,也可以在支持该文档类型的其他app中打开文档,在图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  

  7. interactionControllerWithURL:URL]; 

  8. // Configure Document Interaction Controller

  9.         [self.documentInteractionController setDelegate:self]; 

  10. // Present Open In Menu

  11.         [self.documentInteractionController presentOpenInMenuFromRect:[button frame] inView:self.view animated:YES]; 

  12.     } 

  13. }

为了测试openDocument:方法,在真机上运行实例app非常重要。原因很简单,操作系统要检查安装的app是否支持我们要打开的文件类型(UTI)。如果没有找到支持相应文件类型的app,那么菜单中就不会有打开的提示,而这个不能通过iOS模拟器进行测试。

为了测试这个功能,首先要在真机上安装支持PDF的app,比如Dropbox或者Amazon的Kindle app。

总结

使用UIDocumentInteractionController这个类可以简单地实现app之间文档的预览和打开。建议你去看看这个类的参考文档,特别是UIDocumentInteractionControllerDelegate协议——这里面有许多delegate

方法,当遇到大文档或者复杂的工作流时,这些方法都非常的方便。

源文件:

http://down.51cto.com/data/812631

 

责任编辑:闫佳明 来源: cocoachina
相关推荐

2013-05-30 15:44:45

iOS开发iOS SDKUITextView

2013-05-17 10:54:37

iOS开发iOS SDK调试技巧

2011-07-08 17:45:19

iPhone 文档

2011-08-02 11:07:42

iOS开发 UIWebView

2018-02-06 22:11:41

微软Office 功能

2011-07-06 10:59:14

iOS 4 XCode iPhone

2013-04-27 10:07:51

飞利浦

2012-07-27 09:30:44

Windows Pho

2011-03-03 11:06:04

特性iOS 4.3

2009-07-07 08:51:00

微软Windows 7新功能

2009-09-01 13:13:28

C#打开Word文档

2011-07-29 13:40:00

Xcode iOS 4.2 iPhone

2011-03-18 08:39:28

iOS 4.2 SDKiOS SDK

2011-01-27 10:11:42

Android 3.0

2011-07-26 17:39:53

Xcode iPhone 文档

2011-07-22 10:01:58

IOS SDK Twitter

2011-08-16 15:17:44

IOS SDK

2012-01-01 22:07:28

jQMjQuery MobiHTHL5

2018-02-25 11:22:14

SDK代码接口

2011-02-13 09:40:02

Android 3.0Android 3.0
点赞
收藏

51CTO技术栈公众号