OpenURL does not work in the Share extension

Trying to use [self.extensionContext openURL:... completionHandler:...]; in the extension of iOS 8 Share, to open the containing application, it never opens the application and always calls the completion handler with success = NO.

This is the same problem with Action extensions, but I think it's more wise to open a containing application for Share extensions than Action extensions. The Share extension point is to download potentially most of the data, and the only way to do this without opening the application is through NSURLSession , which can only download HTTP (S). But the application may wish to exchange content using a mechanism other than downloading HTTP (S).

Apple documentation does not indicate that openURL... cannot be used for any particular type of extension. It is hard to see if this is a mistake or an alleged behavior. There is no official information about this.

+6
source share
4 answers

Here are some possible workarounds ... Communicating with / Opening Containing an application from the Share extension

Those that really work can be summarized as:

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

None of these methods are perfect, but they work (not sure if they will like Apple). For more information, you can check the link. Hope this helps :)

Edit: you can also use UIWebView as described by fooobar.com/questions/144323 / ...

+7
source

The documentation states that β€œthe extension point determines whether this method should be supported,” and in practice, only those who use the Today extension can run their host application using openUrl. The provider of Share, Action, Keyboard and Document does not work for everyone (beta 5).

+1
source

I use this method, which does not require any background task:

  • use the UIViewController (instead of the standard SLComposeServiceViewController ) subclass to extend sharing to add a custom layout;
  • Add a UIWebView and load an HTML string with a link pointing to your application’s custom URL;
  • implement the webView:shouldStartLoadWithRequest:navigationType: method webView:shouldStartLoadWithRequest:navigationType: to intercept a click on the link and before returning YES, use NSUserDefaults with initWithSuiteName: (with the name of the application group) to save the data in the container shared by your extension and containing the application.

Clicking on a web link launches an application that can directly receive data in NSUserDefaults .

0
source

This is what I use with the keyboard extension. I hope this helps 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 run this call from the UIInputViewController.

This method should also work using the URL scheme from the containing application.

0
source

Source: https://habr.com/ru/post/972666/


All Articles