I'm trying to preview the SwiftUI screens from my Mac app on the canvas in XCODE 13. In live operation, the model on my app connects to an external device via TCP over the network. I have a an initializer (init()) for my model that initializes the model without network access and I use this in the PreviewProvider in my view file. However, the preview fails to load unless the external device is available on the network. The preview says it is unable to connect to the main application, which makes sense as the main app won't run without the device present.
Why does XCODE have to run the app? and if so, what is the purpose of the PreviewProvider in the view file?
My suggestion then would be to:
- create a framework to host the
View
andPreviewProvider
s - create a dummy app to be used for previews
- have both your real app and the dummy app link the framework
- when attempting to use previews select the dummy app as your active scheme
I believe that should cause the previews system to use the dummy app as the context to render the previews. If that does not work, then another (perhaps easier?) workaround is to skip the framework part and just include the file with the View
in both apps and again select the dummy app as the active scheme before attempting to preview.