Xcode Previews for UIKit not working

I am having an issue when previewing a UIKit ViewController. I am developing my app inside Pods framework which then I import and use in scene/app delegate. I tried to use UIKit to have SwiftUI previews. I conformed to UIViewRepresentable protocol and made a struct that conforms to PreviewProvider where I returned the current controller as some View. I have many external dependencies like Moya, Alamofire, SnapKit...

I am building my components in Pods project. Which I import than into scene/app delegate to start the app.

I am getting the error: "Cannot preview in this file; Unexpected error occurred". I went online and found some people saying that I need to disable Automatically Refresh Canvas setting. But it still doesn't work. I am then getting an error: "Cannot preview in this file; Update failed". I attached a file with a generated report from the error. I also cleaned the build folder and restarted my Mac. I am using Macbook Pro M2.

If I run UIKit previews in a new project it works. But I need it to be able to run in my current project.

https://www.notion.so/Xcode-Previews-UIKit-Error-Report-ba5ec69080f34cb09c6f93c6cd0131d7?pvs=4

Hi,

Sorry to hear you are having problems getting previews working. This error can often indicate a crash in your application when it launches, or a problem with the simulator. If you look into the folder ~/Library/Logs/DiagnosticReports, do you see any crash reports for your app?

If not, the best next step will be to file a feedback with diagnostics so we can take a look.

Steps to generate helpful diagnostics:

  1. Download and install the logging profile on all devices involved. Instructions and profiles are available here: https://developer.apple.com/bug-reporting/profiles-and-logs/?name=swift
  2. Reproduce the issue
  3. Click the "Diagnostics" button in the error banner in Previews' Canvas area (or if the banner is missing you can use the menu: Editor > Canvas > Diagnostics)
  4. In the sheet that appears, click "Generate Report" in the bottom left of the sheet
  5. Attach (or make from the folder) the resulting zip file to the bug (will be named something like previews-diagnostics-0123456789.zip)
  6. Generate the sysdiagnose(s) and attach those too

Maybe it's silly and not related with your issue but I had troubles previewing AppKit controllers too. I solved it by marking the #Preview with an @available statement. Like so:

@available(macOS 14, *)
#Preview {
	let controller = SettingsGeneralViewController()
	return controller
}

Without @available the preview keeps crashing. Maybe it's just me but I think this check should be done automatically...

Xcode Previews for UIKit not working
 
 
Q