1

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?

Quinn
  • 7,072
  • 7
  • 39
  • 61
  • Try to add that framework to the testing target under `target/Build Rules/`. – Fabian Aug 23 '19 at 16:39
  • @fabian not too sure I know where you are talking about... But my test target also includes the framework, builds and passes its test successfully – Quinn Aug 23 '19 at 16:52
  • Ah good then it's something else. I just had two cases where I had to add dependencies to unrelated _Test Targets_ inside the same _Xcode Project_ by adding those Frameworks to `Build Phases/Link Binary With Libraries` to make the preview build without errors. – Fabian Aug 23 '19 at 16:57
  • Did you get it working? – Fabian Aug 24 '19 at 05:00
  • It could be something to do with your preview constructor, correct? – Nerdy Bunz Aug 24 '19 at 10:31
  • @Fabian I didn’t get it working yet – Quinn Aug 24 '19 at 13:20
  • @BooberBunz I’m not sure, do you mean the code in the #if DEBUG? I’ll add that code to the question in a bit if so, but I thinking is okay – Quinn Aug 24 '19 at 13:21
  • Please see a similar question: [Here][1] [1]: https://stackoverflow.com/questions/62141389/swiftui-preview-fails-with-linker-errors/68912088#68912088 – Boaz Frenkel Aug 25 '21 at 14:16

2 Answers2

3

So I had this same issue except it couldn't find my own module... For me the problem was because I have custom build configurations and it didn't like the custom module name. The solution was to rename the module to the same as debug and release in Build Settings > Product Module Name

I hope this helps someone

Manny
  • 1,682
  • 1
  • 14
  • 13
0

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.

Quinn
  • 7,072
  • 7
  • 39
  • 61