检查iOS 7或更早版本的最佳方法?

时间:2013-09-24 21:55:47

标签: ios ios6 ios7

我需要根据我运行的iOS版本重新配置一些UI,所以我需要一种检查iOS版本的好方法。暂时我这样做:

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

我不想硬编码这些字符串比较并根据它做出决定。有没有更好的方法来做到这一点?

2 个答案:

答案 0 :(得分:126)

我总是将它们保存在我的Constants.h文件中:

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

虽然我总是更喜欢上面的宏,但为了完成接受的答案,这里是苹果批准的方式:

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer

}
else {

    // do stuff for older versions than iOS 7
}

答案 1 :(得分:118)

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer

}
else {

    // do stuff for older versions than iOS 7
}