XCODE SwiftUI Preview

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?

Answered by Developer Tools Engineer in 691431022

My suggestion then would be to:

  1. create a framework to host the View and PreviewProviders
  2. create a dummy app to be used for previews
  3. have both your real app and the dummy app link the framework
  4. 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.

Hi,

Sorry to hear you are having problems getting previews working.

The PreviewProvider code teaches the preview machinery how to instantiate your View in isolation rather than in the context of your fall app UI (imagine the PreviewProvider creates just a single cell of your table view). To generate the preview it has to run in some UI context, and very frequently developer's rely on a lot of their other app code for the preview's types and model) so generally that means using your real app as its rendering environment.

All that said, your description of what is happening makes me think that your app is crashing when attempting to generate the preview.

Crash logs for the preview app will show up in ~/Library/Logs/DiagnosticReports/. It can sometimes take a few minutes for it to generate, so give it a bit time and then see if any are there. Hopefully that will give you some helpful hints on what is causing the crash.

If that still doesn't resolve issue then probably best next step will be to file a feedback with diagnostics. We will need the diagnostics Xcode Previews generates in order to make sure we understand the error the previews system is encountering.

  1. When you get an error in Xcode Previews, an error banner appears in the canvas
  2. Click the "Diagnostics" button in that banner
  3. In the sheet that appears, click "Generate Report" in the bottom left of the sheet
  4. Attach (or make from the folder) the resulting zip file to the bug (will be named something like previews-diagnostics-0123456789.zip)
Accepted Answer

My suggestion then would be to:

  1. create a framework to host the View and PreviewProviders
  2. create a dummy app to be used for previews
  3. have both your real app and the dummy app link the framework
  4. 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.

XCODE SwiftUI Preview
 
 
Q