Communicating With/Opening Containing App from Share Extension

How to communicate between iOS App Containing Extension and Extension (not Host App)

TLDR: No, but there's a hack

There's no true interprocess communication for iOS apps, with or without extensions. NSDistributedNotification still hasn't made the trip from OS X to iOS, and probably won't.

With some extension types you can open URLs via NSExtensionContext and use them to pass data to an app that handles the URL. This brings the app to the foreground, which doesn't sound like what you want.

There is a hack that might get you what you need, though.

  • Instead of writing to user defaults, write to a file in your app group directory.
  • Don't just write the file directly-- use NSFileCoordinator to do coordinated writes to the file.
  • Implement NSFilePresenter on an object that wants to know about changes to the file, and make sure to call [NSFileCoordinator addFilePresenter:someObject]
  • Implement the optional presentedItemDidChange method on your file presenter.

If you do all of this right, you can write to this file from either the app or the extension, and then have presentedItemDidChange be automatically called in the other one. As a bonus you can of course read the contents of that file, so you can pass arbitrary data back and forth.

Today's Extension communicate with Main App

You can't communicate directly with your main app as you want. You can basically do two things:

  • Open your main app from your extension
  • Pass data through your shared container

openURL not work in Action Extension

This is by design. We don't want Custom Actions to become app launchers.

Share Extension to open containing app

Currently there's no way to do this. A share extension cannot open the containing app.

The intended approach for share extensions is that they handle all of the necessary work themselves. Extensions can share code with their containing apps by using custom frameworks, so in most cases that's no problem.

If you want to make data available to your app, you can set up an app group so that you have a shared directory. The extension can write data there, and the app can read it. That won't happen until the next time the user launches the app, though.

Can imessage extension open host app?

This is apparently not working in Seed 2 but should be fixed in Seed 3.

https://forums.developer.apple.com/message/153441#153441



Related Topics



Leave a reply



Submit