ios预览和打开文档

来源:互联网 发布:行业差距 知乎 编辑:程序博客网 时间:2024/05/16 05:40


iOS中的沙盒可以让平台更加的安全,这也是沙盒给用户带来的最主要好处。不过由于沙盒的严格限制,导致程序之间共享数据比较麻烦。一般在程序间共享文档可以通过UIDocumentInteractionController(该类经常被开发者忽略)。本文中,我将介绍如何使用这个类在其它程序(已经安装在设备中的程序)中预览和打开文档。


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


UIDocumentInteractionController的使用非常简单。首先通过调用它唯一的类方法interactionControllerWithURL:,并传入一个URL(NSURL),来初始化一个实例对象。之后设置一下这个view controllerdelegate属性,并实现一些恰当的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.}


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的头文件(如下所示)来告诉compilerMTViewController类遵照UIDocumentInteractionControllerDelegate协议。

#import <UIKit/UIKit.h>@interface MTViewController : UIViewController <UIDocumentInteractionControllerDelegate>@end

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


看看previewDocument:方法的实现,首先获得文档的URL (NSURL),由于文档是app的一部分,因此通过NSBundle类获得文档的(NSURL)非常容易,如下:


- (IBAction)previewDocument:(id)sender {       NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];      if (URL) {                // Initialize Document Interaction Controller               self.documentInteractionController = [UIDocumentInteractionController                                                        interactionControllerWithURL:URL];                // Configure Document Interaction Controller                [self.documentInteractionController setDelegate:self];            // Preview PDF                [self.documentInteractionController presentPreviewAnimated:YES];            }    }


如果返回了一个有效的URL,我们初始化一个UIDocumentInteractionController的实例,并且通过文档的URL。在documentInteractionController的属性中我们存储了一个document interaction controller的引用。view controller将会充当document interaction controllerdelegate


如果现在运行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的方式显示文档。


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

    当然你可以简化documentInteractionControllerViewControllerForPreview:的实现以满足你的需要。执行委托方法的同时,你可以首次运行app,试试看这个效果(图6),你可以通过邮件分享这个文档,可以打印或者复制。另外,也可以在支持该文档类型的其他app中打开文档,在图7中点击右边的按钮,看看我说的什么意思。

    

   6

    

   7

    

    Step 4:打开文档

   为了实现这一目的我们需要实现openDocument:方法。在previewDocument:方法中,获取到在程序bundle中一个PDF文件的url,用这个url初始化一个UIDocumentInteractionController

    

   之后设置一下UIDocumentInteractionControllerdelegate,在这个UIDocumentInteractionController中调用presentOpenInMenuFromRect:inView:方法显示一个菜单。传入的第一个参数CGRectbuttonframe,如下所示:

   

 - (IBAction)openDocument:(id)sender {            UIButton *button = (UIButton *)sender;            NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];            if (URL) {                    // Initialize Document Interaction Controller                    self.documentInteractionController = [UIDocumentInteractionController                                                            interactionControllerWithURL:URL];                    // Configure Document Interaction Controller                    [self.documentInteractionController setDelegate:self];                   // Present Open In Menu                   [self.documentInteractionController presentOpenInMenuFromRect:[button frame] inView:self.view animated:YES];               }        }

    

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

    

   为了测试这个功能,首先要在真机上安装支持PDFapp,比如Dropbox或者AmazonKindle app

    

   8

    

   总结

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

    

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

   源文件:

/cms/uploads/soft/130515/4196-130515163Q2.zip