检查设备是否运行 iOS 5 或更高版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7835447/
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
Check if device is running iOS 5 or higher
提问by Alex van Rijs
Possible Duplicate:
Check iPhone iOS Version
可能重复:
检查 iPhone iOS 版本
One of the changes made in iOS 5 is the ability to override the drawrect
methods. This means I need to change the appearance of the navigationBar and tabBar on a different way. I am able to use apple new methods:
iOS 5 中所做的更改之一是能够覆盖这些drawrect
方法。这意味着我需要以不同的方式更改 navigationBar 和 tabBar 的外观。我可以使用苹果的新方法:
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"navigationBarBackgroundRetro.png"] forBarMetrics:UIBarMetricsDefault];
//I create my TabBar controlelr
tabBarController = [[UITabBarController alloc] init];
// I create the array that will contain all the view controlers
[[UITabBar appearance] setBackgroundImage:
[UIImage imageNamed:@"navigationBarBackgroundRetroTab.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:
[UIImage imageNamed:@"tab_select_indicator"]];
I'm developing an app for iOS 4.3 and 5.0. However, iOS 5 ignores the drawrect
method that I'm overriding, so it should run the above code. How can I check the iOS version so I can use the above code if the device is on iOS 5?
我正在为 iOS 4.3 和 5.0 开发一个应用程序。然而,iOS 5 忽略了drawrect
我覆盖的方法,所以它应该运行上面的代码。如果设备在 iOS 5 上,我如何检查 iOS 版本以便我可以使用上面的代码?
回答by Jano
The samples below work for any version number. e.g.: to detect iOS 5 instead 7, replace 7 with a 5 in the code.
下面的示例适用于任何版本号。例如:要检测 iOS 5 而不是 7,请将代码中的 7 替换为 5。
AvailabilityInternal.h macros
AvailabilityInternal.h 宏
This detects the SDK you are building with:
这会检测您正在构建的 SDK:
#ifdef __IPHONE_7_0
// iOS 7.0
#endif
This detects the version set as Deployment Target in the General tab of your target configuration:
这会检测在目标配置的常规选项卡中设置为部署目标的版本:
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
// iOS 7.0 or later
#else
// less than 7
#endif
NSFoundation version
NSFoundation 版本
BOOL isAtLeastIOS61 = NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_6_1;
BOOL isAtMost61 = NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1;
BOOL is7x = floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1;
If you ? click NSFoundationVersionNumber
, you'll see version constants for iOS and OSX. The constant for the current SDK is always missing.
如果你 ?单击NSFoundationVersionNumber
,您将看到 iOS 和 OSX 的版本常量。当前 SDK 的常量始终丢失。
Core Foundation version
核心基础版本
BOOL atLeastIOS61 = kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_6_1;
As with NSFoundationVersionNumber
, the SDK version is missing.
与 一样NSFoundationVersionNumber
,缺少 SDK 版本。
Device system version
设备系统版本
NSString *version = [[UIDevice currentDevice] systemVersion];
BOOL isAtLeast6 = [version floatValue] >= 6.0;
BOOL isAtLeast7 = [version floatValue] >= 7.0;
An alternative way:
另一种方式:
BOOL isAtLeast6 = [version hasPrefix:@"6."];
BOOL isAtLeast7 = [version hasPrefix:@"7."];
An alternative way:
另一种方式:
BOOL isAtLeast6 = [version compare:@"6.0" options:NSNumericSearch] != NSOrderedAscending
BOOL isAtLeast7 = [version compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending
In case of concerns about float/string conversion, let it be know that everything above reports correctly if the version is equal or greater, for any possible iOS version (6.0, 6.0.1, 6.1, etc.).
如果担心浮点/字符串转换,请知道对于任何可能的 iOS 版本(6.0、6.0.1、6.1 等),如果版本相等或更高,上面的所有内容都会正确报告。