VZVirtualMachineView in SwiftUI App

I have a working XIB App to run a Linux VM with graphics interface. I am trying to rewrite it in SwiftUI but fall into all sorts of problems when using a combination of a Representable of the VZVirtualMachineView, an associated Coordinator, and @StateObjects.

a) The VM display is not updated when running but is displayed if I close the window and reopen it. As the underlying VZVirtualMachineView is created/dismantled many times, there are warnings about negative scanouts that end up crashing the App b) Keyboard focus is not really working.

https://developer.apple.com/forums/thread/766014 reports that there is probably a solution making a NSViewControllerReporesentable rather than a VZVirtualMachineViewRepresentable.

I think I am fighting against proper SwiftUI lifecycle and would love to have a hint at what shall be the right organization of model and SwiftUI constructs.

Answered by DTS Engineer in 864961022
I think I am fighting against proper SwiftUI lifecycle

Very likely.

there is probably a solution making a NSViewControllerReporesentable

That’s definitely the path you should explore.

rather than a VZVirtualMachineViewRepresentable.

I’m not sure what VZVirtualMachineViewRepresentable is. It has the VZ prefix, suggesting that it’s from the Virtualization framework, but it’s not actually a class in that framework.

Share and Enjoy

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

I think I am fighting against proper SwiftUI lifecycle

Very likely.

there is probably a solution making a NSViewControllerReporesentable

That’s definitely the path you should explore.

rather than a VZVirtualMachineViewRepresentable.

I’m not sure what VZVirtualMachineViewRepresentable is. It has the VZ prefix, suggesting that it’s from the Virtualization framework, but it’s not actually a class in that framework.

Share and Enjoy

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

I derived a solution from https://github.com/insidegui/VirtualBuddy/blob/main/VirtualUI/Source/Session/Components/SwiftUIVMView.swift

The solution is to create an NSViewController that controls the VZVIrtualMachineView and then wrap it with a Representable.

It would be great to have the Apple Virtualization sample apps use SwiftUI 5.0, as now it is not just an alternative, but what shall be used to develop Apps. Providing XIB samples will just make the transition to best architecture (@Observable, flagged onAppear rather than viewDidLoad...)

I’m happy to hear you’re making progress.

It would be great

I recommend that you make that desire official by file an enhancement request against the sample.

Please post your bug number, just for the record.

Share and Enjoy

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

VZVirtualMachineView in SwiftUI App
 
 
Q