Clicky

iOS Dev Nugget 158 Checking if Newer APIs Are Available In Older iOS Versions (II)

.

Need to run a code review on your codebase? Hire me

In #41 Checking if Newer APIs Are Available In Older iOS Versions, I describe how to check if an API is available in the user's iOS version, using Objective-C. This technique is very useful for switching to alternate implementations or disabling features when your app is running on older iOS versions, yet let you take advantage of newer iOS features (and hardware).

In Swift 2.0, there's an easier and more specific way to detect iOS versions, by using #available. For example, if you want to check if the device supports 3D Touch, but want to support iOS versions earlier than 9.0, you can do this:

if #available(iOS 9.0, *) {
    available = UIScreen.mainScreen().traitCollection.forceTouchCapability == UIForceTouchCapability.Available
} else {
    available = false
}
NSLog("force touch available? \(available)")

#available is compile-time checked, so the Xcode will warn you to use #available when you try to utilize an API that isn't available in older iOS versions.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues