How to check API availability in Swift, iOS / macOS / watchOS / tvOS SDK

What is Availability checking?

Today Apple leads the world of innovation with iOS, MacOS, watchOS and tvOS, and every year we see new operating system release with more enhances, and new technologies.

So all the features Apple releases is coupled with the OS releases (for example Sign-in with Apple is available on iOS 13 and above only). As a developer is highly recommend for us to delivery one binary which support latest as well as old operating system.

So this availability check is Swift gives us the power to check weather the user running a specific version or not.

Swift API to check the OS version?

class LoginViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        initLoginView()
    }

    private func initLoginView() {
        //initialise login view
        
        guard #available(iOS 13.0, *) else {
            return
        }
        
        let authButton = ASAuthorizationAppleIDButton()
        // integrate Sign in with Apple
    }
}

Code snippet shows that we are initializing a LoginView for user to login, and making use of #available API call to check the version, if it’s iOS 13.0 and above we do add “Sign in with Apple” which is only available in iOS 13.0 and above.

Swift also provides us an declaration attribute (there are two kind of attributes in Swift, one that apply to declarations and another to types) to declare that this call or function is available on which iOS version.

@available(iOS 13, *)
func initSignInWithApple() {
// use AuthorizationService framework to integrate Sign in with Apple.
}

These are the arguments we can make use in @available attribute.

  • iOS 
  • iOSApplicationExtension
  • macOS
  • macOSApplicationExtension
  • watchOS
  • watchOSExtension
  • tvOS
  • tvOSApplicationExtension
  • swift

An asterisk (*) to indicate all platforms.

Few interesting use that we can make use.

@available(*, unavailable) – Indicates that the declaration is not available on given platform.

@available(*, introduced) – Indicates the first version when the declaration was introduced.

@available(*, obsoleted) – Indicates the first version platform/language in which the declaration was obsoleted.  

@available(*, message) – A message that the complier displays during the warning or error.

@available(*, renamed) – A message that the complier displays during the warning or error about the renaming.

Interesting example that I really like from Swift documentation.

// A protocol defined in first release.
protocol ConfigProtocol {

}

Would like to change the name in next release?

// change the name.
protocol AppConfigProtocol {

}

@available(*, unavailable, renamed: "AppConfigProtocol") 
typealias ConfigProtocol = AppConfigProtocol 

In this case, complier will start giving you a error, also a fix for you.

‘ConfigProtocol’ has been renamed to ‘AppConfigProtocol’

Replace ‘ConfigProtocol’ with ‘AppConfigProtocol’

Thanks for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.