Is there a way for Xcode to warn about new API calls?
I've actually released something which helps with testing this sort of thing. It's part of my MJGFoundation set of class called MJGAvailability.h.
The way I've been using it is to apply it in my PCH file like this:
#define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED __IPHONE_4_0
#import "MJGAvailability.h"
// The rest of your prefix header as normal
#import <UIKit/UIKit.h>
Then it'll warn (with perhaps a strange deprecation warning) about APIs which are being used that are too new for the target you set as the "soft max" as per the #define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED
. Also if you don't define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED
then it defaults to your deployment target.
I find it useful because I can then double check which APIs I'm using that are too new for the deployment target that I've set.
After digging through AvailabilityInternal.h
, I realized that all available versions above the Deployment target are tagged with the __AVAILABILITY_INTERNAL_WEAK_IMPORT
macro.
Therefore, I can generate warnings by redefining that macro:
#import <Availability.h>
#undef __AVAILABILITY_INTERNAL_WEAK_IMPORT
#define __AVAILABILITY_INTERNAL_WEAK_IMPORT \
__attribute__((weak_import,deprecated("API newer than Deployment Target.")))
By placing this code in a project's precompiled header, any use of an API that might cause a crash on the lowest supported iOS version now generates a warning. If you correctly guard the call, you can disable the warning specifically for that call (modified exmaple from Apple's SDK Compatibility Guide):
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
if ([UIPrintInteractionController class]) {
// Create an instance of the class and use it.
}
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
else {
// Alternate code path to follow when the
// class is not available.
}
On OS X at least, with recent clang/SDK, there is now a -Wpartial-availability
option (add it e.g. in "other warning options")
One can then define the following macros to encapsulate code that handles runtime testing if the method is supported
#define START_IGNORE_PARTIAL _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wpartial-availability\"")
#define END_IGNORE_PARTIAL _Pragma("clang diagnostic pop")
I haven't test on iOS though.
If you use XCode7.3 and above, you can set other warning flag: -Wpartial-availability
, then xcode will show warning for API newer than deployment target version