11

Here is what I have tried that has NOT worked:

  • Using openURL to attempt to open the containing app

Here is what I have thought of that wouldn't work:

  • Using local notifications to provide a link to the containing app (creating the local notification from the extension)

Here are options I am considering:

  • Using a UIDocumentInteractionController and a custom file extension type to provide a link to my containing app (and my containing app only)
  • Starting a "fake" NSURL session to get the following functionality: In iOS, if your extension isn’t running when a background task completes, the system launches your containing app in the background and calls the application:handleEventsForBackgroundURLSession:completionHandler: app delegate method.

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?

livingtech
  • 3,570
  • 29
  • 42
arcticmatt
  • 1,956
  • 1
  • 19
  • 36
  • 1
    Have you also considered the `UIWebView` way? http://stackoverflow.com/a/24614589/102008 – user102008 Aug 12 '14 at 18:32
  • Ah! I've scrolled over that dozens of times but never actually read it thoroughly enough to see what he was talking about. Seems like another working (if not 100% acceptable) workaround. Thanks! – arcticmatt Aug 12 '14 at 18:37
  • 1
    THis is such a good question, how can it be closed. :( I had to search so much for this – Akshansh Thakur Jul 06 '16 at 10:51
  • See that the guy who has closed this also has no experience in ios – Akshansh Thakur Jul 06 '16 at 10:52
  • Have you found a solution for this? I know Pinterest share extension is also opening containing app when user is not authenticated so there must be a way to do this and apple should allow this in review process. – Marcin Kapusta Mar 17 '17 at 23:56
  • @MarcinKapusta Did you ever find a solution to this? – j_d Mar 24 '18 at 13:18
  • @IsaacHinman I can open containing app from share extension. In my example I'm opening app when user is not authenticated but You can open containing app in Your use case. Take a look at `openUrl` utility method and description how to use it here: https://stackoverflow.com/a/43378802/752775 Also please take a note that I don't know if this is working with iOS 10, and iOS 11 because I wasn't tested it on those platforms. I think it should work ;) – Marcin Kapusta Mar 24 '18 at 22:40
  • I voted to re-open this because I think I understand the question and it's a good one, but I think it's missing an elaboration of the question in the opening paragraph. I don't want to put your words in your mouth, but maybe you could add exactly what you're trying to accomplish? – Steven Fisher Dec 01 '22 at 19:00

1 Answers1

5

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.

arcticmatt
  • 1,956
  • 1
  • 19
  • 36