Inter-App Communication for sandboxed swift app on macOS
Asked Answered
P

1

7

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!

Peterec answered 26/5, 2021 at 1:17 Comment(4)
NSXPCConnection should be perfect for this. What problems are you seeing?Springe
Do you have a developer account?Sprightly
@RobNapier I do lack knowledge about XPC. As I understand, XPC service itself not visible for enduser, and even Parent app contains a XPC bundle, the parent shouldn't be able to use the XPC service to initiate a message send request to several child apps. Or can you give me a structure example that how to do it? Thanks.Peterec
@Sprightly Yes I do have a developer account for years and have other apps on Mac App Store.Peterec
B
2

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 request a scripting-targets entitlement or an apple-events temporary exception entitlement. In the same way, regardless of whether your app is sandboxed, any external sandboxed app that attempts to interact with your app must also request the appropriate entitlements to do so."

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.

Benedikt answered 31/5, 2021 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.