I have some SwiftUI code and it seems to work great when I build to a real iOS device or to an iOS simulator.
However in the SwiftUI canvas the preview says Failed to build MySwiftUI.swift
clicking on the diagnostics button next to Try Again
reveals that it failed with the error:
ld: framework not found FrameworkMyAppUses
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have FrameworkMyAppUses
built for simulator and device, and it is in my framework search paths. Building the app to simulator and device both succeed and make use of my framework with no issue, its only the canvas preview that can't build.
If I hit Try again
Xcode will tell me Build Succeeded
but the canvas preview window continues to just display the framework not found error.
Has anyone else experienced this / knows potential fixes?
Finally figured it out!
So it turns out, the reason it couldn't find the library was because I was referencing it with a relative path to a spot outside of my project directory.
My build settings had the Framework Search Paths
set up like this:
../path/to/my/framework
And when I normally built my project this worked fine and it would find the framework. So it seems as though by default my build script is run from my project directory.
When I tried to build for SwiftUI, it must have been doing so from outside my root project directory so the relative framework path so no longer correct and I got the error shown in the question.
Updating the framework search path to a (slightly less relative?) path like this:
$(PROJECT_DIR)/../path/to/my/framework
made it so the framework could be found. Now I can successfully build the project and my SwiftUI canvas updates as it should.