22
votes

I decided to implement a few views using SwiftUI in my app. The app is backward compatible with 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?

3
You linked the framework to your project? I guess it's marked as "Required" and not Weak or optional. On an old version i.stack.imgur.com/tkekO.png found there stackoverflow.com/questions/3352664/…Larme
@Larme I found it was a known issue and posted an answer to solve thisYichenBman

3 Answers

41
votes

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

7
votes

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 the “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.”
-6
votes

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?