Conditionally Import a Framework (Such as Speech) Based on iOS Version in Swift

Conditionally import a framework (such as Speech) based on iOS Version in Swift?

You can make the Framework optional (details and image from Ray Wenderlicht):

Making a framework optional

This, combined with your use of @available, should prevent the system from trying to load it on the devices where it is not available.

Import Framework Based on Current Target

In your target's build settings, Swift Compiler - Custom Flags -> Other Swift Flags, add a flag for one target, say -DTargetX

Then…

#if TargetX 
import FrameworkX
#else
import FrameworkY
#endif

How do you selectively import a framework in Swift?

For Swift <= 4.0 you can use the os() configuration test function:

#if os(iOS)
import CoreTelephony
#endif

You'll have to wrap code that uses CoreTelephony as well.

All available tests for os() are: macOS, iOS, watchOS, tvOS, Linux, Windows, and FreeBSD.

For Swift >= 4.1 you can also use canImport():

#if canImport(CoreTelephony)
import CoreTelephony
#endif

iOS - Link a framework with higher deployment target than my project's

Think about it. The framework could be using features, libraries available in the later deployment of iOS, that aren't in the deployment of iOS you are using. So yes you'll have to change your app's deployment target if you want to use this framework.

Although you can conditionally import a framework based on iOS version Conditionally import a framework (such as Speech) based on iOS Version in Swift? but then of course you'll need a solution for the versions of iOS which the framework doesn't work for.



Related Topics



Leave a reply



Submit