Best Way to check for iOS 7 or earlier? [duplicate

2019-03-07 23:37发布

This question already has an answer here:

I need to reconfigure some UI based on the iOS version I am running against, so I need a good way of checking the iOS version. For the time being I am doing this:

if ([[[UIDevice currentDevice] systemVersion] isEqualToString: @"7.0"]) {
    //do stuff
}

I'd prefer not to hard code these string comparisons and make decisions based on that. Are there any better ways of doing this?

标签: ios ios6 ios7
2条回答
We Are One
2楼-- · 2019-03-07 23:44

I always keep those in my Constants.h file:

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) 
#define IS_OS_5_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
#define IS_OS_6_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
#define IS_OS_8_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#define IS_OS_9_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)

Although I'll always prefer the above macros, for the completion of the accepted answer, here is the apple approved way:

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer

}
else {

    // do stuff for older versions than iOS 7
}
查看更多
家丑人穷心不美
3楼-- · 2019-03-07 23:50
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer

}
else {

    // do stuff for older versions than iOS 7
}
查看更多
登录 后发表回答