IOS APP间 互相唤醒 并传递文件

来源:互联网 发布:怎么找淘宝优惠券 编辑:程序博客网 时间:2024/05/21 19:44

需求:

由于开发的需求,现第三方的APP 想调用我们的APP,并在我们的APP中打开第三方的文件。

难点:

苹果每个APP都会生成在一个单独的沙盒中,每个沙盒都是独立的。互相之间没有权限访问到彼此沙盒中的内容。

走过的坑:

尝试过传递文件路径-----> 失败。尝试过传递数据Data----->失败。尝试过...............------------>失败。

面包会有的,牛奶也会有的,问题终究会解决的。

在论坛里看到猿友 提到啦UIDocumentInteractionController,忽然想起来之前一篇博客http://blog.csdn.net/heroguo_jp/article/details/51134308 。感觉又点儿小思路啦,但是还不确定能不能走通。

充电~~~~~~

参看啦苹果官方文档
https://developer.apple.com/reference/uikit/uidocumentinteractioncontroller 后,确定啦可执行性,开始码代码。

UIDocumentInteractionController 的使用

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

step2:在.m中船舰一个方法,来响应你点击某个按钮时弹出popView,在popView中可以罗列出,可以打开该文件的对应App应用,是不是很酷呢?
代码如下:

    // filePath 你想要用其他应用打开的文件路径    documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];    documentController.delegate = self;    // .UTI 表示所能支持的第三方文件打开的类型    documentController.UTI = @"com.sunsetlakesoftware.molecules.stl";    // 弹出的试图的样式儿    [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中的文件后缀名去动态设置这个值。也可以自定义奥,参考
https://developer.apple.com/reference/uikit/uidocumentinteractioncontroller

UIDocumentInteractionControllerDelegate里面的方法自己看看,基本可是知道是什么意思。

~~~~~~~~~华丽的分割线~~~~~~~~~~~

以上都是需要在自己的APP中设置的。那么如何在被打开App中设置呢?
只需要一句话,在info.plist 中加入如下部分

<key>CFBundleDocumentTypes</key>    <array>        <dict>            <key>CFBundleTypeName</key>            <string>MoleculesStructureFile</string>            <key>CFBundleTypeRole</key>            <string>Viewer</string>            <key>LSHandlerRank</key>            <string>Owner</string>            <key>LSItemContentTypes</key>            <array>         <string>com.sunsetlakesoftware.molecules.stl</string>            </array>        </dict>    </array>

另外在APPDelegate中实现如下方法

- (BOOL)application:(UIApplication *)application            openURL:(NSURL *)url  sourceApplication:(NSString *)sourceApplication         annotation:(id)annotation{    // str 文件的路径      NSString *str = [[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    if (str.length >0)    {        return YES;    }else        return NO;}

看明白啦么? 不明白继续看
http://blog.csdn.net/heroguo_jp/article/details/51134308

PS:个人整理的方法,如有好的方法欢迎共享。

0 0