zoukankan      html  css  js  c++  java
  • iOS SDK:预览和打开文档

    http://mobile.tutsplus.com/tutorials/iphone/previewing-and-opening-documents-with-uidocumentinteractioncontroller/

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


     
    Step 2: 创建用户界面
    这个程序的用户界面包括两个按钮,一个是用于在其他app中预览PDF文档,另一个是用户在其他app中打开PDF文档。创建用户界面之前,在viewcontroller执行文件中为每个按钮赋予一个动作,如下:
    1.- (IBAction)previewDocument:(id)sender {
    2.}
     
    1.- (IBAction)openDocument:(id)sender {
    2.}

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

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

    使用UIDocumentInteractionController类注意事项:1. 你需要保存着document interation controller的实例。2.需要实现UIDocumentInteractionControllerDelegate协议。
     
    首先更新viewcontroller的头文件(如下所示)来告诉compilerMTViewController类遵照UIDocumentInteractionControllerDelegate协议。
    1.#import <UIKit/UIKit.h>
    2.@interface MTViewController : UIViewController<UIDocumentInteractionControllerDelegate>
    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 InteractionController
    5.        self.documentInteractionController= [UIDocumentInteractionController 
    interactionControllerWithURL:URL];
    6.        // Configure Document InteractionController
    7.        [self.documentInteractionControllersetDelegate:self];
    8.        // Preview PDF
    9.       [self.documentInteractionController presentPreviewAnimated:YES];
    10.    }
    11.}
     
    如果返回了一个有效的URL,我们初始化一个UIDocumentInteractionController的实例,并且通过文档的URL。在 documentInteractionController的属性中我们存储了一个document interaction controller的引用。view controller将会充当document interaction controllerdelegate
     
    如果现在运行app,你会注意到点击预览按钮时,什么都没有发生。因为这里我们首先要实现一个delegate method
     
    前边提到,UIDocumentInteractionController类并不是UIViewController的子类,而是继承自 NSObject我们需要通知documentinteraction controller使用哪个view controller进行文档预览。
     
    UIDocumentInteractionControllerDelegate
    中有一个名documentInteractionControllerViewControllerForPreview:delegate方法,该方法请求获得一个用于显示(预览)文档的viewcontroller
     
    我们希望在main viewcontroller中显示预览,所以可简单的返回self,如下代码所示。它的意思是document interfation controller将使用我们的view controller来预览PDF文档——modal view的方式显示文档。
     
    1.- (UIViewController *)documentInteractionControllerViewControllerForPreview: 
    (UIDocumentInteractionController *) controller {
    2.    return self;
    3.

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

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

     

    之后设置一下UIDocumentInteractionController delegate,在这个UIDocumentInteractionController中调用 presentOpenInMenuFromRect:inView:方法显示一个菜单。传入的第一个参数CGRectbuttonframe,如下所示:
    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 InteractionController
    6.        self.documentInteractionController= [UIDocumentInteractionController 
    interactionControllerWithURL:URL];
    7.        // Configure Document InteractionController
    8.       [self.documentInteractionController setDelegate:self];
    9.        // Present Open In Menu
    10.        [self.documentInteractionControllerpresentOpenInMenuFromRect:[button frame] inView:self.view animated:YES];
    11.    }
    12.}

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

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


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

    源文件:

     

    http://download.csdn.net/detail/ydj213/5405941

     

  • 相关阅读:
    python依赖包整体迁移方法
    ubuntu关于ssh协议登录问题
    k8s部署02-----kubeadm部署k8s
    k8s部署01-----what is k8s?
    兼容到ie10的js文件导出、下载到本地
    webstorm减少内存占用
    foxmail占cpu 100%解决办法
    原生js返回顶部
    js字符串驼峰和下划线互相转换
    element-ui的rules中正则表达式
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3165733.html
Copyright © 2011-2022 走看看