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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18992766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:39:47  来源:igfitidea点击:

Best Way to check for iOS 7 or earlier?

iosios6ios7

提问by Huang

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:

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

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?

我不想对这些字符串比较进行硬编码并基于此做出决定。有没有更好的方法来做到这一点?

回答by Segev

I always keep those in my Constants.h file:

我总是将它们保存在我的 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)

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
}

回答by Ar Ma

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {

    // do stuff for iOS 7 and newer

}
else {

    // do stuff for older versions than iOS 7
}