秒懂iOS文件分享
2017-04-22 18:39
54 查看
iOS的文件分享可分为两种:
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功能 如图所示
![](http://img.blog.csdn.net/20170422183736167?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzUzODU0Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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了。是不是会很简单
![](http://img.blog.csdn.net/20170422183840262?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzUzODU0Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
这是Apple官网的文档 https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
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了。是不是会很简单
这是Apple官网的文档 https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
相关文章推荐
- Google 发布新版Google Drive for iOS应用,新增卡片式缩略图布局,文件分享更方便
- 如何为iOS app添加AirDrop文件分享功能
- iOS将App做成可在文件分享打开中出现
- 关于ios的文件分享功能
- iOS将HTML页面转换成PDF文件保存到本地并分享传输文件 - 简书
- iOS开发 实现原生分享 可以跳过选择面板,分享多个文件
- 移动开发经验分享1:IOS应用文件下载及打开
- 【幻灯片分享】揭开IPA文件的灰沙 -- 通过静态分析工具了解IPA实现 | 友盟 张超 | iOS DevCamp
- 【分享】兼容ie6-9和现代浏览器以及ios,android,pad等触屏设备的异步文件上传插件
- IOS 分享相册里面的视频文件
- iOS分享 - AFNetworking之多图片/文件上传
- 如何为iOS app添加AirDrop文件分享功能
- iOS开发 - 打开文件后进行文件分享(QLPreviewController)
- 【iOS_GitHub】文档/文件查看器(支持本地或者其他app分享过来的word、excel、pdf、rtf等格式文件)
- iOS系统预览文件但不分享的实用技巧 (iOS10, QLPreviewController)
- IOS开发教程之put上传文件的服务器的配置及实例分享
- IOS开发教程之put上传文件的服务器的配置及实例分享-备用
- [绍棠] 如何为iOS app添加AirDrop文件分享功能
- iOS通过百度mp3接口解析mp3文件地址代码分享
- iOS分享 - AFNetworking之多图片/文件上传