Is there a way for XCode to warn about new API cal

2019-01-03 08:32发布

On more than one occasion I've seen crashing bugs appear on iOS 3.x due to use of a new call that was introduced in 4.x without proper checking.

Is there a way for XCode to warn about classes, methods and procedures that are only available a later version than the deployment target?

That way I could easily list through all the code and make sure it's properly conditionalized.

9条回答
ゆ 、 Hurt°
2楼-- · 2019-01-03 09:30

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.

查看更多
老娘就宠你
3楼-- · 2019-01-03 09:30

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.
    }
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-03 09:34

No, there is no such warning. However, when you use new API (since you are obviously writing these later), just check the docs when they were available.

Also, if you're supporting 3.0 and using new SDK for development, you must absolutely be testing on actual devices running 3.0

Another thing you could do is write your own utility that parses the availability macros in the headers and then warns you if you're calling anything you shouldn't be.

However, I must reiterate, if you're targeting an older version and using the newer SDK, you must check the docs to see when API became available, and test appropriately.

查看更多
登录 后发表回答