Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for API availability in Xcode 9

I'm using he UserNotification framework that is available only in iOS 10. I am declaring a method that uses this framework and so far, I have been doing the check for the availability as follows:

@interface MyService : NSObject
 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
    -(BOOL)handleWillPresentNotification:(UNNotificationContent *)notificationContent;
#endif
@end

XCode 9 beta was just release and with this code I get a warning

'UNNotificationContent' is partial: introduced in iOS 10.0 Annotate 'handleWillPresentNotification:withCompletionHandler:' with an availability attribute to silence

The question is how do I annotate this in Objective C code the entire method? I know that Xcode 9 introduced the

if (@available(iOS 10, *)) {
    // iOS 10 ObjC code
}

but how do I wrap the entire method (including its signature) in it?

cheers

like image 866
Jan Avatar asked Jul 25 '17 11:07

Jan


People also ask

What is API in Xcode?

An API, or application programming interface, is used to pass data back and forth between software apps in a formalized way.

What is #available in Swift?

Availability checking in Swift gives us the ability to ask whether the user is running a specific or newer version of an operating system, and run code only if that test passes. This allows us to use the latest functionality from iOS, macOS and so on, while also degrading gracefully for users on older iOS versions.

What is API in Swift iOS?

API stands for Application Programming Interface. It is a programmatic way to be able to access functions and data of other apps.


Video Answer


1 Answers

to answer my own question: I need to mark the method using the NS_AVAILABLE_IOS macro as follows:

-(BOOL)handleWillPresentNotification:(UNNotificationContent *)notificationContent NS_AVAILABLE_IOS(10_0);
like image 163
Jan Avatar answered Oct 16 '22 14:10

Jan