Day 7: Checking API availability in Swift

Max
2 min readJan 31, 2018
Photo by Brad Starkey on Unsplash

Tell me if this sounds familiar, your super duper App has a legacy support so it runs on from iPhone 4s to the latest iPhone and you want to implement some functionality which was pain for previous versions but is simple as ______ mmmm cannot catch any reference atm lets just say simple 👅 for later versions.

So you want to ready to write that painful piece of code but also want to use new APIs available for the latest versions.

Caption is already written above 😆

Here swift can give you a hand…

Swift has built-in support for checking API availability, which ensures that you don’t accidentally use APIs that are unavailable on a given deployment target. You use an availability condition in an if or guard statement to conditionally execute a block of code, depending on whether the APIs you want to use are available at runtime.

Syntax

Example

So the body of if statement executes for iOS 10 or later and macOS 10.12 or later. The last argument * is required and signifies any other platform.

You use platform names such as iOS, macOS, watchOS, and tvOS—for the full list, see.

Reference

That’s one question, see you tomorrow. If you ❤️ this article please give it a clap👏 and support my work. Also, smash that Follow icon 🟢 to get posts delivered to you on your couch. We are in this together 😇. Cheers!

Psst! 👋, want to continue? Go to next article in the series or the previous one, or if you wish to start from beginning.

--

--

Max

Senior iOS Engineer | Swift | ObjectiveC | SwiftUI | Combine |