Communicating with/opening Containing app from Share extension [closed]

半腔热情 提交于 2019-11-28 06:35:26

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.

This method requires 3 main steps, as follows:

  1. Make a background NSURLSession in a Share Extension.
  2. Start a download task.
  3. Call exit(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);

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!