How to define a preprocessor macro for checking iOS version

I use it to check the version of iOS, but it does not 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 do

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

Nothing happens. Is something wrong here?

+5
source share
6 answers

You have defined a macro, but use it in a mode other than a macro. Try something like this, with the same macro definition.

IF_IOS5_OR_GREATER(NSLog(@"iOS5");)

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

+5
source

Much simpler:

#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
+13
source
#ifdef __IPHONE_5_0 

..

. c

+8
source

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 this method.

+5
source

To check the runtime, 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 for it in UIDevice, 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];
+2
source
#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0
+1
source

All Articles