UIDocumentInteractionController 第三方应用中打开自己的文件 方法

http://blog.sina.com.cn/s/blog_6d64a8df01015m6w.html

听起来好复杂,其实很简单,在你的.h文件中添加UIDocumentInteractionControllerDelegate


然后在.m文件中,新建一个方法,用来响应你点击按钮弹出时弹出popView,其中会罗列可打开当前文件的其他应用
我的方法如下

-(void)openDocumentIn{

    

    documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:unEncodingURL]];

    documentController.delegate = self;

    documentController.UTI = @"com.microsoft.word.doc";

    [documentController presentOpenInMenuFromRect:CGRectMake(760, 20, 100, 100) inView:self.view animated:YES];

}

其中的 documentController.UTI 表示那些类型的文件支持第三方软件打开,此链接里面有各种类型文件对应的参数 https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html 可用根据unEncodingURl中的文件后缀名去动态设置这个值


rect 根据自己的需求写

然后delegate里面的方法自己看看应该能明白是什么意思

就不多说了




PS:此法必须使用真机来测试,而且真机测试前最好先下载几个可以支持你所需文件的软件,比如我就安装了WPSOFFICE ,而且,最好做个判断,当读取的可用软件为空时,给用户一个提示,要不然用户会丈二和尚的⋯⋯


https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/PreviewingandOpeningItems.html#//apple_ref/doc/uid/TP40010410-SW1

Previewing and Opening Files

When your app needs to interact with files it cannot preview or open on its own, use a UIDocumentInteractionController object to manage those interactions. A document interaction controller works with the Quick Look framework to determine whether a file can be previewed in place, opened by another app, or both. Your app, in turn, works with the document interaction controller to present the available options to the user at appropriate times.

To use a document interaction controller, do the following:

  1. Create an instance of the UIDocumentInteractionController class for each file you want to interact with.

  2. Provide a representation of the file in your app’s user interface. (Typically, you would do this by displaying the file name or an icon)

  3. When the user interacts with the file representation, such as by tapping it, ask the document interaction controller to present one of the following interfaces:

    • A file preview that displays the contents of the file.

    • A menu containing options to preview the file or open it using another app. You can add copying and printing options to this menu by implementing appropriate methods in the delegate of the document interaction controller.

    • A menu prompting the user only to open it using another app.

    A document interaction controller provides built-in gesture recognizers that makes implementing these actions straightforward.

Any app that interacts with files can use a document interaction controller. The most likely candidates to need these capabilities are apps that download files from the network. For example, an email app might use document interaction controllers to preview or open files attached to an email.

Document interaction controllers are also useful for some apps that do not download files. If your app supports file sharing, for example (see UI State Preservation and theDocInteraction sample code project), you can use a document interaction controller with a file that was synced to your app’s Documents/Shared directory.

Creating and Configuring a Document Interaction Controller

To create a new document interaction controller, initialize a UIDocumentInteractionController instance with the file you want it to manage, and assign a delegate object. The delegate is responsible for providing the controller with information it needs to present its views and, optionally, for performing additional actions when those views are displayed or the user interacts with them.

The following code creates a new document interaction controller and sets the delegate to the current object. Note that the caller of this method needs to retain the returned object.

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL) fileURL
    usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
 
    UIDocumentInteractionController *interactionController =
        [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;
 
    return interactionController;
}

Once you have a document interaction controller, you can use its properties to get information about the associated file including its name, type, and URL. The controller also has an icons property that contains UIImage objects representing the document’s icon in various sizes. All this information can be useful when representing the file in your user interface.

If you plan to let the user open the file in another app, you can use the annotation property of the document interaction controller to pass custom information to the opening app. It is up to you to provide information in a format that the other app will recognize. For example, this property can be used by app in an application suite. When one app wants to communicate additional information about a file to other apps in the suite, it does so by way of the annotation property. The opening app sees the annotation data as the value associated with the UIApplicationLaunchOptionsAnnotationKey key of the options dictionary that is passed to it.

Presenting a Document Interaction Controller

You can use a document interaction controller to display a file preview or to prompt the user to choose an action for a file.

Each of these methods attempts to display a view—either a document preview or a menu. When calling any of these methods, check the return value. A return value of NO indicates that the requested view would have contained no content, and so is not displayed. For example, the presentOpenInMenuFromRect:inView:animated: method returns NO if there are no installed apps capable of opening the file.

If you call a method to display a file preview, your delegate object must implement the documentInteractionControllerViewControllerForPreview: method. Previews are displayed modally, so the view controller that you return from this method becomes the parent for the preview. If you do not implement this method, if your implementation returns nil, or if the view controller that your implementation returns is unable to present another modal view controller, the document preview is not displayed.

A document interaction controller automatically handles the dismissal of the view it presents. However, you can dismiss the view programmatically as needed by calling thedismissMenuAnimated: or dismissPreviewAnimated: methods.

For sample code that demonstrates how to present a document interaction controller using gesture recognizers, view the DocInteraction sample code project.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值