Easy way to detect if I am using framework not available to older iOS

ios, ios-frameworks, xcode

Solution

Take a look at my `MJGAvailability.h` here on GitHub. Basically it defines the right preprocessor `#define`s so that newer APIs will look like they're deprecated, so you get a compiler warning. There's some documentation at the top of the file, but basically you just do this in your PCH file:

#define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED __IPHONE_4_3
#import "MJGAvailability.h"

For if you want to support down to iOS 4.3.

It's certainly not 100% foolproof, but I find it incredibly useful for doing what you're asking.

If you want to suppress a warning because maybe you know that it's OK to use that API (you've surrounded it with a `respondsToSelector` for example) then you can do something like this:

UINavigationBar *navBar = self.navigationController.navigationBar;
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [navBar setBackgroundImage:[UIImage imageNamed:@"navbar_bg.png"] forBarMetrics:UIBarMetricsDefault];
#pragma clang diagnostic pop
}

Problem

After a marathon coding session, where I added too much code without keeping track of everything, I now have an app that probably has some iOS 5.0 and 5.1 specific enums, calls and frameworks, however, I want my app to support iOSes back to 4.3. I was hoping that there was an easy way of setting Xcode to compile as if it were compiling for iOS 4.3, so that I would get errors for all of the offending code that needs to be conditioned out, and/or alternatively coded, for older iOS. I thought I'd get that by using the compiler option: ``` -D__IPHONE_OS_VERSION_MAX_ALLOWED=__IPHONE_4_3 ``` but that ends up generating error in system header files, not my code. Given that most enums and frameworks have their availability included in the header files, I have to think that there is an easy way to do what I need. Has anyone managed to do such a thing without resorting to downloading older Xcodes with old SDKs? There I may run into the issue of Xcodes that won't function properly under Lion (which is what I am running). UPDATE: It appears as though I can't install Xcode 3.2.6 on Lion. I now will have to find a Snow Leopard Mac, unless I find a way to use compiler options or forcing Xcode to use old SDKs... Here is a sample of what @mattjgalloway's answer did for me: Lumin App Group /Users/mz/Dev/Working/Lumin/Lumin/MyUIScreen.m - 'brightness' is deprecated: TOO NEW! - 'brightness' is deprecated: TOO NEW! - 'brightness' is deprecated: TOO NEW! - 'brightness' is deprecated: TOO NEW! /Users/mz/Dev/Working/Lumin/Lumin/LuminViewController+Share.m - 'TWTweetComposeViewController' is deprecated: TOO NEW! - 'TWTweetComposeViewController' is deprecated: TOO NEW! - 'TWTweetComposeViewController' is deprecated: TOO NEW! /Users/mz/Dev/Working/Lumin/Lumin/LuminViewController.m - 'scrollView' is deprecated: TOO NEW! - 'connectionWithMediaType:' is deprecated: TOO NEW! - 'connectionWithMediaType:' is deprecated: TOO NEW! - 'AVCaptureDeviceSubjectAreaDidChangeNotification' is deprecated: TOO NEW! - 'setSubjectAreaChangeMonitoringEnabled:' is deprecated: TOO NEW! Very nice. I placed the following in my project's .pch file, and am planning on doing so for all projects: ``` #if DEBUG #define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED __IPHONE_OS_VERSION_MIN_REQUIRED #import "MJGAvailability.h" #endif ``` For any project, I am now automatically watching for SDK issues based on the earliest iOS I am targeting. While there still may be SDK changes I have to worry about, at least I know of most framework additions that are unavailable to an older iOS release.

Original source