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:
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: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.
1) the container application needs to listen as an XPC server for connections from other tasks
I also don't follow the next part: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.
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;
At this point I think the task is beyond me and I could really use a sample project to study.- and which can open its own connexion to the container.
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!