Recommended / Canonical way to host remote (separate process) SwiftUI views.

I am building a tool that enables the user to write, auto-compile and interact with SwiftUI code (think something like a mini Xcode Canvas). Which so far works really well.

The app is not sandboxed since it uses tools like swiftc and sourcekit-lsp.

The obvious problem here is that since the 'Preview' part of the app is driven by arbitrary code a crash/hang there would lead to a termination of the whole app.

I understand that there are some private apis like NSRemoteView or CALayerHost but I would like to avoid them if I can.

From what I see reading other similar solutions IOSurface sharing + event forwarding might be the best solution.

So my question is: Is there a proper or recommended way to achieve this? Meaning having a fully interactive SwiftUI view presented in my host app but running on a separate process?

Any pointers to the right direction or examples or whatever could help me with this would be greatly appreciated.

Answered by DTS Engineer in 850066022

Implementing remote view support is hard. I’m actually good friends with the person who maintains the internal macOS support for this and, yeah, he has some stories to tell (-: So, I wouldn’t recommend that you go down this path:

From what I see reading other similar solutions IOSurface sharing + event forwarding might be the best solution.

You might be able to make it work in some cases but it’s gonna be a lot of work and it’ll likely be quite brittle.

The most promising alternative is to implement this using ExtensionKit. It has high-level support for hosting the extension’s view within your app’s view hierarchy.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Implementing remote view support is hard. I’m actually good friends with the person who maintains the internal macOS support for this and, yeah, he has some stories to tell (-: So, I wouldn’t recommend that you go down this path:

From what I see reading other similar solutions IOSurface sharing + event forwarding might be the best solution.

You might be able to make it work in some cases but it’s gonna be a lot of work and it’ll likely be quite brittle.

The most promising alternative is to implement this using ExtensionKit. It has high-level support for hosting the extension’s view within your app’s view hierarchy.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you Quinn (big fan btw),

I have tried to do it with an extension, which works surprisingly well, but for some reason the only way to get it to truly work was to present the remote view (tried both NSView/Controller representables) inside a popup.

Trying to directly include it in the hierarchy results in a stale view (shows up the content but cannot be interacted with).

Trying to directly include it in the hierarchy results in a stale view

Weird. User interface stuff isn’t my forte, but I would’ve expected that to work.

AFAIK Apple doesn’t have ExtensionKit sample code, but I know that various third-party folks have used it. One of the Apple development blogs I follow recently talked about this, and that post includes links to their open source project.

https://www.massicotte.org/extensionkit-intro

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you once again 🙏 I managed to get it to work (there is very little information indeed).

Recommended / Canonical way to host remote (separate process) SwiftUI views.
 
 
Q