使用外部应用打开pdf

http://www.jianshu.com/p/f1b2b39b2c7b


1 在你的应用对某个文档如PDF提供可以选择第三方应用的列表来打开该文件功能

2 让自己应用有能力出现在别人应用的文件打开选择列表里

现在分开介绍这两种功能对于你的应用来说需要如何设置:

第一种只需要调用系统的UIDocumentInteractionController就OK了:

-(void)openDocumentIn {

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Courses for Q2 2011" ofType:@"pdf"];

UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];

documentController.delegate = self;

[documentController retain];

documentController.UTI = @"com.adobe.pdf";//You need to set the UTI (Uniform Type Identifiers) for the documentController object so that it can help the system find the appropriate application to open your document. In this case, it is set to “com.adobe.pdf”, which represents a PDF document. Other common UTIs are "com.apple.quicktime-movie" (QuickTime movies), "public.html" (HTML documents), and "public.jpeg" (JPEG files)

[documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

}


第二种即共享文件 导入导出 导入的文件夹放在Document下面的Inbox文件夹里

1 UIFileSharingEnabled 设置为YES

并设置如下的格式告诉系统你能处理这个类型的文件

2 设置info.plist里添加可以读取PDF功能 如图所示


3 当第三方应用选择你的程序后,回自动触发– application:openURL:sourceApplication:annotation:. 你要做的就是在该方法里实现打开这个PDF即可:

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[webView setUserInteractionEnabled:YES];

[webView loadRequest:requestObj];这样就可以打开来自其它应用让你处理的文件了如PDF文件

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[webView setUserInteractionEnabled:YES];

[webView loadRequest:requestObj];这样就可以打开来自其它应用让你处理的文件了如PDF

4 但如果你要打开自定义的文件格式则需要告诉系统你的自定义格式 UIExportedTypeDeclarations 并新增一个item 可能你会问为什么PDF不用设置这个,因为系统已经默认支持PDF所以不用在设置一个PDF对应的UIExportedTypeDeclaration。那你依然如图所示添加就OK了。是不是会很简单。




文/SevenJustin(简书作者)
原文链接:http://www.jianshu.com/p/f1b2b39b2c7b
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

猜你喜欢

转载自blog.csdn.net/gdutxzy/article/details/51719186