How to define preprocessor macro to check iOS version

本秂侑毒 提交于 2020-01-10 19:41:51

问题


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?


回答1:


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.)




回答2:


Much simpler:

#define IS_IOS6_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)



回答3:


#ifdef __IPHONE_5_0 

etc

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




回答4:


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.




回答5:


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];



回答6:


#define isIOS7 ([[[UIDevice currentDevice]systemVersion]floatValue] > 6.9) ?1 :0


来源:https://stackoverflow.com/questions/7836967/how-to-define-preprocessor-macro-to-check-ios-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!