Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inter-App Communication for sandboxed swift app on macOS

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!

like image 888
Xiao Xiao Avatar asked May 26 '21 01:05

Xiao Xiao


People also ask

Can Swift be used for macOS apps?

Swift is free and open source, and it's available to a wide audience of developers, educators, and students under the Apache 2.0 open source license. We're providing binaries for macOS and Linux that can compile code for iOS, macOS, watchOS, tvOS, and Linux.

Are Mac App Store apps sandboxed?

It is designed to contain damage to the system and the user's data if an app becomes compromised. Apps distributed through the Mac App Store must adopt App Sandbox. Apps signed and distributed outside of the Mac App Store with Developer ID can (and in most cases should) use App Sandbox as well.

Does SwiftUI work on Mac?

SwiftUI runs on iOS 13, macOS 10.15, tvOS 13, and watchOS 6, or any future later versions of those platforms.


Video Answer


1 Answers

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:

  • Apple Event Manager
  • AppleScript Overview (explains in detail macOS scripting architecture)
  • Apple Events Programming Guide (full reference for building, sending and receiving Apple Events)

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.

like image 195
jvarela Avatar answered Nov 15 '22 09:11

jvarela