6

I use it to check iOS version, but it doesn't work:

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_5_0
#define kCFCoreFoundationVersionNumber_iPhoneOS_5_0 675.000000
#endif

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
#define IF_IOS5_OR_GREATER(...) \
if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_5_0) \
{ \
__VA_ARGS__ \
}
#else
#define IF_IOS5_OR_GREATER 0
#endif

when I make

#if IF_IOS5_OR_GREATER
NSLog(@"iOS5");
#endif

nothing happens. Is something wrong here?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Bartosz Bialecki
  • 4,321
  • 10
  • 40
  • 63
  • Do you want this to check whether a user of your app is running iOS 5? If so, you want something like James' method. A preprocessor macro won't help you with that, as it runs at compile-time. – Chris Devereux Oct 20 '11 at 14:14
  • There are no runtime checks in your code, so how on earth is it supposed to know the current iOS version? All you are doing is checking the version of the SDK that your code was compiled with. – Mike Weller Oct 20 '11 at 14:15
  • Err @MikeWeller (and Chris), I'm afraid neither of you are correct. The macro indeed does create a runtime check if the compile time conditional is met. The OP was simply calling the macro incorrectly. See here for a full explanation of how this works: http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html – followben Jan 31 '12 at 01:44

6 Answers6

14

Much simpler:

#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
Gowiem
  • 1,156
  • 15
  • 18
  • 1
    This has the drawback that you can't use it outside of code blocks, for example in header files, etc. – eremzeit Sep 09 '15 at 23:47
10
#ifdef __IPHONE_5_0 

etc

Just look for that constant. All the objective c constants start with two underscores

kirb
  • 1,996
  • 1
  • 17
  • 28
Joel Teply
  • 3,140
  • 1
  • 28
  • 21
  • This is a preprocessor macro, meaning it's executed when the app is being compiled, rather than on the user's device. Also, a correct answer was already selected nearly a year ago... – kirb Jul 10 '12 at 16:10
  • This macro is useful to separate the code in compiling time with `#ifdef` and `#ifndef`, all the other answers are only available in the runtime with `if` statement usage. – Itachi Nov 08 '18 at 09:42
5

Define this method:

+(BOOL)iOS_5 {
    NSString *osVersion = @"5.0";
    NSString *currOsVersion = [[UIDevice currentDevice] systemVersion];
    return [currOsVersion compare:osVersion options:NSNumericSearch] == NSOrderedAscending;
}

Then define the macro as that method.

James
  • 2,326
  • 1
  • 16
  • 18
  • NSOrderedAscending will make this method return false if the current os is >= 5.0. You should use NSOrderedDescending – matzahboy Jun 28 '12 at 01:31
5

You've defined a macro, but you're using it in the non-macro way. Try something like this, with your same macro definition.

IF_IOS5_OR_GREATER(NSLog(@"iOS5");)

(This is instead of your #if/#endif block.)

Cajunluke
  • 3,107
  • 28
  • 28
2

For a runtime check use something like this:

- (BOOL)iOSVersionIsAtLeast:(NSString*)version {
    NSComparisonResult result = [[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch];
    return (result == NSOrderedDescending || result == NSOrderedSame);
}

If you create a category on UIDevice for it, you can use it as such:

@implementation UIDevice (OSVersion)
- (BOOL)iOSVersionIsAtLeast:(NSString*)version {
    NSComparisonResult result = [[self systemVersion] compare:version options:NSNumericSearch];
    return (result == NSOrderedDescending || result == NSOrderedSame);
}
@end

...

if([[UIDevice currentDevice] iOSVersionIsAtLeast:@"6.0"]) self.navigationBar.shadowImage = [UIImage new];
dankrusi
  • 86
  • 2
1
#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0
Sarthak Patel
  • 129
  • 2
  • 8