您的位置:首页 > 移动开发

使用第三方APP打开本地文档 (UIDocumentInteractionController)

2013-06-07 13:55 639 查看
基于IOS的沙盒设计原理,我们并不能随心所欲的去访问IOS文件系统中的每一个文件。用户的文件管理权限是很低的,一般情况下,用户只能通过当前应用来访问其沙盒目录下的文件,而无法访问其他应用程序沙盒内的资源。

但是,苹果也提供了一些方法来实现有限的文件共享功能。

一、设置应用程序的“FileSharing”,将应用程序沙盒内的Document目录开发给用户。文件共享允许用户通过iTunes向应用程的
Documents
目录添加、删除文件。

这一功能的实现很简单,只需在应用程序的
Info.plist
文件中添加
UIFileSharingEnabled
键,并将键值设置为
YES
。但是需要注意一点,此时用户的Document是开发目录,里面的所有文件都是用户可以进行删除操作的,为了安全起见,对于应用程序的相关配置文件可以放在Libary目录下。

二、使用UIDocumentInteractionController实现APPs间的本地文档共享。UIDocumentInteractionController可以用来预览文档,以及使用第三方的app来打开当前app中的文档(UIDocumentInteractionController会获取系统中所有申明可以打开当前文件类型的第三方应用,并显示在其menu中共选择,但是并不能保证一定可以打开该文件)。

- (IBAction)pdfBtnPressed{

NSString *path = [[NSBundle mainBundle] pathForResource:@"Weibo_IOS_SDK" ofType:@"pdf"];

NSURL *url = [NSURL fileURLWithPath:path];

UIDocumentInteractionController * document = [UIDocumentInteractionController interactionControllerWithURL:url];

document.delegate = self;

[document presentPreviewAnimated:YES];

}

//delegate

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{

return self;

}





如果想要让自己的应用可以打开第三方应用沙盒内的文档,则需要操作系统申明你可以打开的文件类型,并在应用程序启动的回调函数中实现相应的逻辑

1. 向ios申明(在plist里声明: document types)app能够打开某种类型的文档,这样其他app才可能通过DIC(document interaction interface)把文件转给你app来打开

2. 在application:didfinishelaunchingwithoptions中处理文件路径

NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];


Document Type: https://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html

参考文章:

/article/2048590.html

http://www.cocoachina.com/newbie/basic/2013/0515/6212.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: