available()

The available() function provides the same functionality as the if #available language syntax in Swift, but makes it accessible to all languages.

Syntax:

method available(params platforms: array of String): Boolean;
bool available(params String[] platforms);
func available(_ platforms: String...) -> Bool
bool available(String... platforms);

available() can be used to protect code that will only work on specific newer versions of the platform, by specifying the minimum platform version required. Like if #available, it serves three purposes at once:

  1. It returns a boolean that can be used to check the platform version at runtime.
  2. In combination with an if statement, it will tell the compiler to automatically omit any Deployment Target warnings in the code that is protected by the if available clause.
  3. Also inside an if statement, it tells the compiler to ignore the code when building for a lower Target SDK.

For the second part to have affect, the call to assigned() must be the only condition in the if statement, or the first of multiple conditions combined with the logical and (Oxygene) or && (C# and Swift) operator.

available() expects a list of strings as parameter. Each string must be a platform name, followed by a space and a version number. The version number can consist of one, two or three parts (i.e. "iOS 9", "iOS 9.1" or "iOS 9.0.1"). Platform names can be "OS X", "iOS", "watchOS" and "tvOS", and are not case sensitive.

If a platform is not represented in the list, available() will return for this platform, unless the list of platforms is terminated with a wildcard ("*") string. For example, the code below checks for a specific iOS and OS X version. When compiled for run on watchOS or tvOS, the code inside the if statement will run regardless of version, because these two platforms are being covered by *.

Please refer to the Deployment Targets topic for further discussion on this.

Example

if available("iOS 12.0", "macOS 10.14", "*") then begin
  // code that requires the new OS.
end;
if (available("iOS 12.0", "macOS 10.14", "*"))
{
  // code that requires the new OS.
}
if available("iOS 12.0", "macOS 10.14", "*") {
  // code that requires the new OS.
}
if (available("iOS 12.0", "macOS 10.14", "*")) {
  // code that requires the new OS.
}
  • The code inside the if statement will only be executed if the platform requirements (in this case, iOS 12 or later, or macOS 10.14 or later) are met.
  • Any deployment target warnings the code inside the if statement would normally emit will be suppressed, because the compiler "knows" that the code will not run on the lower deployment targets.
  • When building the project for a Target SDK lower than iOS 12 or macOS 10.14, the code will actually be treated as if it was #ifdef'ed out.

As such, available() makes it easy to create apps build for a new Target SDK that can regress gracefully on older systems and to also (optionally) keep your project able to build against the older Target SDK.

This is particularly helpful during the beta phase of a new Apple OS, when Apple does not allow shipping apps built with the new SDK yet: you can start adopting new APIs in your code, and test them locally and in TestFlight beta builds when you build with the new SDK, and you can still build your app against the old SDK, to submit interim builds to the store.

Swift

Note that in Swift,

if available("iOS 9.0", "OS X 10.11") {

is equivalent to the Swift-specific #available syntax. Both versions will work in Swift.

if #available(iOS 9.0, OS X 10.11) {

Under the hood, available() calls the __ElementsCocoaPlatformAndVersionAtLeast() (Toffee) or __ElementsPlatformAndVersionAtLeast() (Island) helper function defined in the Toffee Base Library and Island RTL.

Cocoa Only

  • available() is currently supported on Cocoa and Android only.

See Also