ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
2017-08-09 16:19
549 查看
一.自己的APP调用第三方打开文件
主要是使用UIDocumentInteractionController类 并实现
UIDocumentInteractionControllerDelegate的代理方法
@interface ViewController ()<UIDocumentInteractionControllerDelegate> @property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController; @end - (void)viewDidLoad { [super viewDidLoad]; //url 为需要调用第三方打开的文件地址 NSURL *url = [NSURL fileURLWithPath:_dict[@"path"]]; _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url]; [_documentInteractionController setDelegate:self]; [_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; }
需要在真机上调试,例子中打开的是 doc文件,如果手机上装了WPS或者office套件,就能调用这些应用打开。
当然,如果你在模拟器上也安装了一个马上要介绍的“第三方调用自己的APP打开文件”的应用,也可以在模拟器上试试。
二.第三方APP调用自己的APP,打开文件
1.在info.plist
中添加如下代码
<key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeName</key> <string>com.myapp.common-data</string> <key>LSItemContentTypes</key> <array> <string>com.microsoft.powerpoint.ppt</string> <string>public.item</string> <string>com.microsoft.word.doc</string> <string>com.adobe.pdf</string> <string>com.microsoft.excel.xls</string> <string>public.image</string> <string>public.content</string> <string>public.composite-content</string> <string>public.archive</string> <string>public.audio</string> <string>public.movie</string> <string>public.text</string> <string>public.data</string> </array> </dict> </array>
这在系统中添加了参数,如果有以上类型的文件,第三方应用可以调用我们的APP进行操作。
2.在第三方调用我们的APP后,会调用如下方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation { if (self.window) { if (url) { NSString *fileNameStr = [url lastPathComponent]; NSString *Doc = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/localFile"] stringByAppendingPathComponent:fileNameStr]; NSData *data = [NSData dataWithContentsOfURL:url]; [data writeToFile:Doc atomically:YES]; self.alert.message = @"文件已存到本地文件夹内"; [self.alert show]; } } return YES; }
注:
url 就是第三方应用调用时文件的沙盒地址, @"Documents/localFile" 表示本地文件夹目录 sourceApplication 是调用我们APP的第三方应用是谁 我们把url传到我们需要用的界面 可以使用路径查看保存到本地的文件
相关文章推荐
- ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件 - 哈哈的笑 - 博客园
- ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
- ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
- iOS APP中第三方APP调用自己的APP,打开文件
- 第三方APP 调用自己的APP,传输文件
- 利用UIDocumentInteractionController文档预览以及调用iPhone里第三方相关的app打开文档
- iOS开发笔记--调用打开AppStore显示自己的App
- webView打开本地文件和使用第三方网页,以及使用自己的webViewClient
- iOS 调起第三方程序打开文件 ( UIDocumentInteractionController )
- 打开操作,以及如果没有默认格式,弹出打开方式让用户自己选择何种程序打开文件
- iOS 第三方应用中打开自己的文件(UIDocumentInteractionController)
- ios中通过UIDocumentInteractionControllers调用第三方应用打开本地压缩文件
- iOS开发笔记--调用打开AppStore显示自己的App
- iOS App让自己的应用在其他应用中打开列表中显示、iOS把自己的应用添加到”活动“、将PDF文件Open In MyApp
- iOS 里面如何使用第三方应用程序打开自己的文件,调用wps其他应用打开当前应用里面的的ppt doc xls
- VC调用其他程序以及用其他程序打开相应文件
- iOS App让自己的应用在其他应用中打开列表中显示、iOS把自己的应用添加到”活动“、将PDF文件Open In MyApp
- 调用已经安装的APP来打开自己应用中的文件
- Delphi调用外部程序打开文件
- ShellExecute 调用记事本程序打开txt文件