问题描述
以下是我尝试过的无法使用的内容:
Here is what I have tried that has NOT worked:
- 使用openURL尝试打开包含的应用
以下是我所想到的不起作用:
Here is what I have thought of that wouldn't work:
- 使用本地通知提供包含应用的链接(从扩展程序创建本地通知)
以下是选项I我在考虑:
Here are options I am considering:
- 使用UIDocumentInteractionController和自定义文件扩展名类型提供指向我的包含应用程序的链接(以及我的包含应用程序)
- 启动虚假NSURL会话以获得以下功能:在iOS中,如果后台任务完成时您的分机未运行,系统会在后台启动您的应用程序并调用应用程序:handleEventsForBackgroundURLSession:completionHandler:app委托方法。
UIDocumentInteractionController已确认为wor k在Xcode 6.5中,但流程有点不稳定。 NSURL的东西应该也可以,但它也有点可疑。有没有人有任何其他想法让我的包含应用程序从共享扩展中打开,或者想要从共享扩展中与它通信?
The UIDocumentInteractionController is confirmed to work in Xcode 6.5, but the flow is kind of wonky. The NSURL thing should work as well, but it's also a bit fishy. Does anyone have any other ideas of getting my containing app to open from a share extension, or an idea to communicate with it from a share extension?
推荐答案
我已经确认NSURLSession方式(上面考虑选项下的第二个子弹)确实有效。我还在努力解决一些问题,但这里有基础知识。使用此方法,您确实可以从共享扩展程序中打开您的应用。
I have confirmed that the NSURLSession way (second bullet under the "considering" options above) indeed does work. I'm still working out some kinks, but here are the basics. Using this method, you can indeed open your app from a share extension.
此方法需要3个主要步骤,如下所示:
This method requires 3 main steps, as follows:
- 在共享扩展中创建后台NSURLSession。
- 开始下载任务。
- 呼叫退出(0)。
确保您下载的内容需要足够长的时间才能在下载任务完成之前终止扩展。
Make sure the thing you are downloading takes long enough so that the extension terminates before the download task finishes.
NSString *address = @"https://googledrive.com/host/0B5zObXR9UzgmbFpob2J5eXpjNXc/file3m";
self.mySession = [self configureMySession];
NSURL *url = [NSURL URLWithString:address];
NSURLSessionTask *myTask = [self.mySession downloadTaskWithURL:url];
[myTask resume];
exit(0);
然后,在您的包含应用程序的UIApplicationDelegate类中,实现
Then, in your containing application's UIApplicationDelegate class, implement the
application:handleEventsForBackgroundURLSession:completionHandler:
方法。在您的扩展程序终止后下载任务完成时,将调用此方法。然后,在这个方法中,你可以调用
method. This method gets called when the download task finishes after your extension has been terminated. Then, in this method, you can call
[[UIApplication sharedApplication] openURL:url];
或在您的收听应用中执行其他操作。
or do some other stuff in your containing app.
此方法的主要问题是,在扩展终止的时间与包含应用程序启动的时间之间存在延迟。与UIDocumentInteractionController方法相比,此方法的主要优点是不需要额外的用户交互。我将继续尝试更多细节。
The main problem with this method is that there is a delay between the time when the extension terminates and the time when the the containing app starts up. The main advantage of this method over the UIDocumentInteractionController method is that no extra user interaction is needed. More details will come as I continue to experiment.
这篇关于与共享扩展程序中的/打开包含应用程序进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!