How to import a particular framework only if iOS 13 is available?

Viewed 2555

Our app supports iOS 12 and up, however we have two new local frameworks "Jello" and "Wizdom" within the app, but they only support iOS 13 and later (so they can use Combine and SwiftUI).

The app weakly links against Jello, Wizdom, Combine, and SwiftUI. The app's main BaseUI module is what actually imports Jello. BaseUI supports iOS 12 and up so it also weakly links against Jello etc.

However we're running into a problem where BaseUI won't compile because of the error: Compiling for iOS 12.0, but module 'Jello' has a minimum deployment target of iOS 13.0.

How can I make the import conditional on iOS 13 being available (i.e. don't import it if iOS 13 is not available)?

(So far the only workaround I've found is to create an Objective C wrapper around Jello and access it through that wrapper, but this is awkward and kludgy. I also tried wrapping that whole file with #if canImport(SwiftUI) ... #endif however this also doesn't work because it seems that this is just a compile-time check.)

1 Answers

Have you try to conditionally import the framework as explained in this post : Conditionally import a framework (such as Speech) based on iOS Version in Swift?? There is a full explanation here However, the main information is that you can define in the framework as optional under the “Targets” section -> "Build Phases" -> "Link Binary With Libraries" -> "Status" of the framework not available on iOS 12.

You also have to wrap the code using this library with the @available tag.

Related