if (@available(macCatalyst 15.0,*)) vs if (@available(iOS 15.0, *))

Experimenting with Mac Catalyst a bit. I see sometimes the compiler warns me about an API and suggests this fix:

  if (@available(macCatalyst 15.0,*))
 {
    /// Use API for this version of catalyst....
}

And when I switch back to run on an iOS device the warning comes back and this time I get the suggestion:

if (@available(iOS 15.0, *))
 {
     //iOS 15
}

If I just use the @available iOS 15 check and remove the macCatalyst check the compiler stops warning me when I try building on my Mac and on iOS, but the same isn't true the other way around (if I have code inside the macCatalyst version check, switching to build for an iOS device still warns me).

I was expecting that code inside the if @available(macCatalyst 15,*) statement would only run on Mac catalyst if the version of macOS supports the API, but would not run on iOS at all (even if the API is available there too) but I still get compiler warnings on iOS and a suggestion for an extra if (@available(iOS 15.0, *)) which produces the following:

 if (@available(macCatalyst 15.0,*))

    {
        if (@available(iOS 15.0, *))
        {
            //do something iOS 15 and later...and macCatalyst  15 and later.
        }
}

What do I make of the macCatalyst availability check? It comes up in code completion but just using the iOS check seems like the correct way (it silences compiler warnings but I haven't tested on an older version of macOS to see if using any of this API would produce a crash).

I conclude that if (@available(iOS 15.0, )) should be used pretty much always instead of if (@available(macCatalyst 15.0,)) and if I need code only for Mac Catalyst I should check the TARGET_OS_MACCATALYST macro instead:

 #if TARGET_OS_MACCATALYST
#else
//iOS only
#endif

Is that correct?

Answered by Frameworks Engineer in 732394022

In general APIs available in macCatalyst are also available in iOS, so you should prefer the iOS check whenever possible.

Accepted Answer

In general APIs available in macCatalyst are also available in iOS, so you should prefer the iOS check whenever possible.

if (@available(macCatalyst 15.0,*)) vs if (@available(iOS 15.0, *))
 
 
Q