Checking API Availability in Swift

Check you can use it, ok?

Image for post
Image for post
Photo by Keagan Henman on Unsplash

Developing against an Operating System means that you might be limited in the features of Swift or the SDK that you can use.

What can you possibly do then?

This article will explain a solution.

Difficulty: Beginner | Easy | Normal | Challenging


  • Coding in Swift Playgrounds (guide HERE)


API: Application programming interface. A set of accessible tools for building software applications

API Availability

Here we are talking about attributes to allow you to support multiple versions of iOS with ease.

To help you do this, Swift provides availability attributes. We shall explore how these work, and how they can be implemented into your own code to make a great coding experience for your colleagues, and a great App experience for your users.

Functions with a Minimum iOS Version

You might design your API for your client to use (perhaps in a framework, or similar).

When you want to remove an old function you can’t just remove it. Perhaps there are still users of the older version, and you would like to gently nudge them to using a newer version (that is, you wish to deprecate a function).

Functions can indeed be marked exactly like that. In the following example we would like to deprecate test(), and replace with newTest().

@available(iOS, deprecated:10.0, message:"Use NewTest()")
func test() {
print ("test")
func newTest() {
print ("New test")

Now when the code is run, a nice little warning is displayed in Xcode telling the user the situation.

This is a nice thing, and of course newTest() has no such message

Image for post
Image for post

Run a Block of Code, if available

If a certain version of a platform version is available, you can check through #available(platform version, *) where the asterisk is used as a wildcard to cover additional platforms (including those that might not yet exist) that are not included in Swift’s list of platforms — and must be included.

if #available(iOS 12, OSX 10.4, *) {// Statements to execute if iOS 12 or OSX 10.4 is available} else {// Fallback statements to execute otherwise}

Check Version, Using a Guard

If a certain version is not available, you can use a guard — but do remember that the wildcard * needs to be used in the same way as stated above.

guard #available(iOS 12, watchOS 52.0, *) else {

Make it Unavailable

You might be sadistic (possible), and like to cause problems. Alternatively you might like to leave pointers for your developers after functions have been deprecated, and in this case you can make functions unavailable. No problem.

And you can use this (of course, as you could with the functions above) with variables or functions.

Image for post
Image for post

The @avaliable options:

Image for post
Image for post

unavaliable: Should be used where functions are no longer avaliable

introduced:Specifies the version and the platform where the code was first avaliable

deprecated: Tells us when a function will be deprecated (version number), but allows us to use the original code

obsoleted: Specifies the version number when this code was obsoleted.

message: Allows a String message to be written to the API user

renamed: Tells us the new name of the function or variable. Allows the user to use the code fix tool in Xcode.


You may be lucky in that you can specify the OS that your users can have to be able to use your App. You might even choose to limit them to the latest version of Swift and the iOS SDK.

However, in real life you probably have a boss, and they want you to serve the maximum number of customers that it is possible to — increasing your market.

The best way of approaching this? Check the availability of features and go from there.

This article has (hopefully) helped you and and given you a way of tackling this common problem.

The Twitter contact:

Any questions? You can get in touch with me HERE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store