Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dyld: Library not loaded SwiftUI when app runs on iOS 12 using @available(iOS 13.0, *)

Tags:

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?

like image 984
YichenBman Avatar asked Sep 12 '19 13:09

YichenBman


People also ask

Does SwiftUI support iOS 12?

SwiftUI runs on iOS 13, macOS 10.15, tvOS 13, and watchOS 6, or any future later versions of those platforms. This means if you work on an app that must support iOS N-1 or even N-2 – i.e., the current version and one or two before that – then you will be limited in terms of the features you can offer.

What is the latest version of SwiftUI?

SwiftUI 3 is still in beta and will become available with iOS 15 and Xcode 13. There is much more to SwiftUI 3 than can be covered here, so do not miss the What's new in SwiftUI video from WWDC 2021 if you are interested.

Is SwiftUI same as Xcode?

Xcode and Swift are both software development products developed by Apple. Swift is a programming language used to create apps for iOS, macOS, tvOS, and watchOS. Xcode is an Integrated Development Environment (IDE) that comes with a set of tools that helps you build Apple-related apps.


2 Answers

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

like image 152
YichenBman Avatar answered Sep 23 '22 16:09

YichenBman


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.”
like image 22
Kacper Dziubek Avatar answered Sep 23 '22 16:09

Kacper Dziubek