openURL doesn't work in Share extension

左心房为你撑大大i 提交于 2019-12-05 05:59:45
arcticmatt

Here are some possible workarounds... Communicating with/opening Containing app from Share extension

The ones that actually work can be summed up as:

  • Using a UIDocumentInteractionController and registering your container app with a special extension type to open your container app from your extension
  • Using a dummy NSURLSessionTask to get the application:handleEventsForBackgroundURLSession:completionHandler: method of the UIApplicationDelegate class of your container app to be called. Then, in that method, you can call [[UIApplication sharedApplication] openURL:url] to open whatever you want.

Neither of these methods are perfect, but they do work (not sure if Apple would like them though). For more details you can check out the link. Hope this helps :)

Edit: You can also use a UIWebView as described https://stackoverflow.com/a/24614589/3923882

The documentation does indicate that the "extension point determines whether to support this method" and in practice only those who use the Today Extension are able to launch their host app using openUrl. Share, Action, Keyboard, and Document provider do not work for anyone (beta 5).

I'm using this method which does not involve any background task:

  • use a UIViewController (instead of the default SLComposeServiceViewController) subclass for share extension in order to add custom layout;
  • add a UIWebView and load an HTML string with a link pointing to your app's custom URL;
  • implement webView:shouldStartLoadWithRequest:navigationType: method to intercept the click on the link and before returning YES use NSUserDefaults with initWithSuiteName: (providing the name of the app group) to save data in the container shared by your extension and the containing app.

The click on the web link launches the app, which can fetch the data in NSUserDefaults directly.

This is what I use from my keyboard extension. I hope it helps here too:

UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
NSString *urlString = @"https://itunes.apple.com/us/app/watuu/id304697459";
NSString * content = [NSString stringWithFormat : @"<head><meta http-equiv='refresh' content='0; URL=%@'></head>", urlString];
[webView loadHTMLString:content baseURL:nil];
[self.view addSubview:webView];
[webView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.0];

Please note that in this case I am instantiating this call from the UIInputViewController.

This method should also work using the URL scheme from the containing app

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