Swift UI Preview Crash

Any help with preview crash on an App that is building just fine and runs on simulator fine?

Are there logs that I can access somehow?

The previews were working just fine, but then stopped and I have reverted all changes since and the previews are still broken.


Post not yet marked as solved Up vote post of brendenk1 Down vote post of brendenk1
15k views

Replies

When I say the Preview Crash - I mean the UI displays a black screen, red X, and the prompt of "Preview Crash". It is not breaking on trying to build the preview but when running the preview via the "play" button.
  • Уже поздно конечно, но все же - у меня сегодня встретилась такая же проблема, не понимал в чем дело, пролазил весь интернет в поисках ответа и все бесполезно. потом пробить встроенный терминал и оказалось, что ошибка была в моем коде, конкретно на ссылке URL я поставил пробел в самом начале htttps-запроса. после того как убрал пробел все заработало как часики, очень странно что xcod не ругался и выкидвал ошибку

Add a Comment
Howdy,

Sorry to hear you are having problems getting previews working. 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.
Based upon your description of the situation I am guessing that the preview version of the app has some stale app data (perhaps an old version of a CoreData container that doesn't have migration support to the current version?). To start from a fresh slate you can use the terminal app and run xcrun simctl --set previews delete all.

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 using CoreData in my app and the terminal command solved my preview problem. Thx.

Add a Comment
Thanks for the response!

The terminal command did not fix the issue.

Unfortunately there is no banner that appears when I click the play button above the device preview. The preview just goes to an all black screen with a red circle and x with the words "Preview Crashed".

For clarity this is only effecting the live preview


Hi friend, I managed to fix the problem for my self. Have you tried to change the preview device? If that doesn't work try restarting xcode and then changing the preview device.
It worked for me.
Hope it worked for you too.
Try to comment out the didFinishLaunchingWithOptions method in AppDelegate , hope this should fix the issue . you application may be referring to external frameworks !!

Has a fix been found for this? Facing the same issue currently.

I am having the same problem with Preview crashing, but not on all files. However, I actually get a crash screen with OK/Reopen. It's a fairly standard Login screen with AppAuth 1.4.0 from package.

Would be thankful for any help as it is painful to run up app in simulator for UI changes.

I had the same problem.

In my case it was the following:

I had an ObservableObject in the App subclass that I was passing through environmentObject() to the content view.

I had forgotten to add a corresponding call to environmentObject() in ContentView_Previews.previews

Once I added that back in it stopped crashing.


struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView().environmentObject(Item())

    }

}

I hope it helps :-)

  • Yes, this help. Preview broken!

Add a Comment

I know it's been 3 years, but I found a solution to this issue. If the previews for your project were working just fine, and you started randomly getting preview crashes for certain content views and pages, then it is most likely a programmatic error. Nothing is wrong with your code, but you need to clean and reset the content view code. Then, once the content view preview reloads without crashing, you can paste back the old code and everything should return and work smoothly!

  • Great, that was working for me

Add a Comment

Same thing happened to me. The hint in the error log was about app signing. I had just renamed my app and one of the residuals was that the Xcode set the app to be signed locally instead of "Developer". The app signing was set to "Developer" before the app rename, so I don't know why Xcode set that to a different value. Setting it back to "Developer" fixed the problem for me.

This happened to me too, just changed the preview device "iPhone 15 Pro" to "iPhone 15"