I'm building a sandboxed macOS app with Swift, which contains a child app inside.
What I want to implement is:
- Parent can launch multiple child apps
- Parent send different content to each child app to show
- Both parent and child app have their own UIs.
Implementation way I've been thinking about:
Distributed Notification
Distributed notification with user info objects are denied by sandbox.
CFMessagePort
It requires both parent and child app are in the same app group, and signed with proper provision profiles. But on Xcode it is always None required
on the provision profile settings so nowhere to change the settings.
Besides, there is no documents or posts explaining how to use CFMessagePort in Swift. I tried the code below but it crashes everytime as it's denied by sandbox.
let portName = "{team_identifier}.{app_group_identifier}.{port_name}"
let remote = CFMessagePortCreateRemote(nil, portName as CFString)
var returnData: Unmanaged<CFData>? = nil
if kCFMessagePortSuccess == CFMessagePortSendRequest(remote, 0, data as CFData, 1, 1, CFRunLoopMode.defaultMode.rawValue, &returnData) && nil != returnData {
}
NSXPCConnection
I don`t think XPC works for this situation, as XPC is designed to communicate between a service running invisible and a client app, while the service is bundled into the app. I doubt it would work for the parent-child model.
So, is there any better way to achieve my target? I feel I should go with CFMessagePort but I also need some more help on how to use it with Swift. Thanks!
I think your best bet would be to use Apple Events, as I do. Sandboxed apps can receive and send Apple Events to themselves by default, but they cannot send Apple Events to other applications. However, they can send Apple Events if
You can read all about it on the Technical Q&A note QA1888.
I would also recommend that you check this tutorial to handle Apple Events in a sandboxed app and this tutorial (part 1 and part 2) how to handle the foundation classes you can use to build Apple Events in Swift.
If you want a more in-depth explanation and including sample code (however, it is mostly in C), you can refer to Apple's references pages, namely:
If you run into trouble, let us know as I use this old but proven technology to handle most IPC between my main app and its helpers.