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?

Accepted Reply

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.

  • Thanks for the reply. I see your answer addressed my question, but in this case,I think there was something else wrong with the design of the application, but I don't know what, I'm still learning about observable objects and environment objects. When I restructured the app to clean up those, the problem went away.

Add a Comment

Replies

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)
  • I'm sure my app is crashing. My question is not why the app is crashing - I know it is crashing because it can't communicate with the external device (I will eventually need to fix that). Is there a way to get the PreviewProvider to use a UI context other than the main app? Or, am I missing the point of Previewing? I realize I don't have a "live" preview, but I don't need one. I just want to see (and adjust) how the various parts of the views look as I adjust the layout and options.

Add a Comment

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.

  • Thanks for the reply. I see your answer addressed my question, but in this case,I think there was something else wrong with the design of the application, but I don't know what, I'm still learning about observable objects and environment objects. When I restructured the app to clean up those, the problem went away.

Add a Comment