How (in detail) can I pass a message from my App Extension to my host app?

Host → App Extension


Passing messages from Host → App Extension is easy. Just call:

dispatchMessage(withName:toExtensionWithIdentifier:userInfo:completionHandler:)

... on the host, and listen for it in the App Extension using:

messageReceivedFromContainingApp(withName:userInfo:)

😌👌

App Extension → Host


Passing messages in the opposite direction is far harder.

The ability to communicate both ways between App Extension and Host was first introduced in the WWDC Talk Whats New in Safari Extensions (16:55, 18:36). It says that both Host and App Extension need to be in the same App Group, and can then share UserDefaults and/or establish an XPCConnection.

The latest WWDC talk Meet Safari Web Extensions confirms that these are still the only ways to share messages in this direction.

How to send messages over XPC


What I know


This is far from a basic task, so here are the resources I've found.
  • objc.io has an article on XPC, which the forums won't allow me to link to, that includes a helpful sample project. However, it does not demonstrate message-passing between a Host and an Extension; just between Host and XPC Service.

  • Apple's Creating XPC Services guide

  • 3-year-old unresolved Apple Developer Forum post asking the same thing as me

  • There are two articles out there on how to create XPC Services in Swift, but I can't link them.

What I don't know


I've followed these instructions in the above-mentioned Apple Developer forum post:

Anyway, to add a contained XPC service to an extension is pretty straightforward — just use the standard Xcode “New / Target” menu item, select “XPC Service” from the panel, and finish that stuff.

However, I'm not really sure what to do now that I have this XPC Service target set up. Moreover, another comment suggests that I might need to get launchd involved, which is another thing I don't know a thing about:

1) the container application needs to listen as an XPC server for connections from other tasks

Far as I know (based on some excellent Quinn's advice), this, alas, is possible only if the container application happens to be a login item; otherwise the XPC system does not allow for that.

I also don't follow the next part:

2) the extension, being sandboxed, cannot directly open an XPC connexion to the container application; nevertheless, it can contain its own XPC service:

- to which a connexion is open automatically;

- and which can open its own connexion to the container.

At this point I think the task is beyond me and I could really use a sample project to study.

Please help


All I want is to send a message from my App Extension back to my Host app. This all seems desperately hard, and I can't find any sample code demonstrating how to pass messages both ways between Hosts and App Extensions over XPC. I think the WWDC sessions over the years really glossed over this and we could do with a proper end-to-end resource here.

Thank you for any help!
Bump

How (in detail) can I pass a message from my App Extension to my host app?

If you don’t get a response via informal channels, like DevForums, my advice is that you open a DTS tech support incident and discuss this with our Safari extension specialist.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
How (in detail) can I pass a message from my App Extension to my host app?
 
 
Q