dyld: Library not loaded SwiftUI when app runs on

2020-08-10 07:48发布

问题:

I decided to implement a few views using SwiftUI in my app. The app is backwards compatible to iOS 12.

Everything works perfectly until I run it on an iOS 12 device. The app crashes immediately and the warning I get says SwiftUI cannot be loaded.

dyld: Library not loaded: /System/Library/Frameworks/SwiftUI.framework/SwiftUI
  Referenced from: /var/containers/Bundle/Application/MyApp.app/MyApp
  Reason: image not found

I'm using @available(iOS 13.0, *) in all the correct places and there are no compiler warnings and the app runs perfectly on iOS 13

How can I get this to work for iOS 12?

回答1:

Turns out this is a known issue and apple introduced a new build setting flag to handle it

Apps containing SwiftUI inside a Swift package might not run on versions of iOS earlier than iOS 13. (53706729)

Workaround:

When back-deploying to an OS which doesn't contain the SwiftUI framework, add the -weak_framework SwiftUI flag to the Other Linker Flags setting in the Build Settings tab. See Frameworks and Weak Linking for more information on weak linking a framework. This workaround doesn't apply when using dynamically linked Swift packages which import SwiftUI.

Adding -weak_framework SwiftUI to Other Linker Flags fixed my issue



回答2:

You can also mark SwiftUI.framwerk as optional in Build Phases. Detailed Instruction below.

  1. Select your project file in the Xcode navigator.
  2. Select your app target (or whichever target you're using SwiftUI from).
  3. Select the “Build Phases” tab, then “Link Binary With Libraries” section.
  4. Click the + button under the list that appears.
  5. Type “SwiftUI” into the search field of the popup sheet, select “SwiftUI.framework,” and click “Add.”
  6. On the new row in the table, click the “Required” popup button in the last column. Change the value to “Optional.”


回答3:

So sorry, but I don't think SwiftUI Will work on older versions then iOS 13. Found this stack link if you want more details Is SwiftUI backwards-compatible with iOS 12.x and older?