Detecting available API iOS vs. watchOS in Swift Detecting available API iOS vs. watchOS in Swift ios ios

Detecting available API iOS vs. watchOS in Swift


If you want to execute that code only on iOS, then use #if os(iOS) instead of the if #available(iOS ...).

This way, you are not using a dynamic check for the version of your operating system, but are compiling a different code for one OS or the other.


In the Apple dev guide, it is said that the star, * (which is required) means that it will execute the if body for OSes not specified but listed in the minimum deployment target specified by your target.

So, if your target specifies iOS and watchOS, your statement if #available(iOS 9.0, *) means that the ifbody is available for iOS 9 and later and any watchOS version.

Also, be careful if you want to use what's described in the chapter "Build Configurations" in this Apple guide. It is used to conditionally compile your code based on the operating system. This is not dynamic at runtime.


With the GM version of Xcode7 I think they fixed that issue. For me :

if #available(watchOS 2,*) {    // Only if using WatchOS 2 or higher}

is working fine in GM version.