我们如何以编程方式检测设备运行的 iOS 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7848766/
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
How can we programmatically detect which iOS version is device running on?
提问by meetpd
I want to check if the user is running the app on iOS less than 5.0 and display a label in the app.
我想检查用户是否在低于 5.0 的 iOS 上运行应用程序并在应用程序中显示一个标签。
How do I detect which iOS is running on user's device programmatically?
如何以编程方式检测用户设备上运行的是哪个 iOS?
Thanks!
谢谢!
回答by Marek Sebera
Best current version, without need to deal with numeric search within NSString is to define macros
(See original answer: Check iPhone iOS Version)
最好的当前版本,无需在 NSString 中处理数字搜索是定义macros
(请参阅原始答案:检查 iPhone iOS 版本)
Those macros do exist in github, see: https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
这些宏确实存在于 github 中,请参阅:https: //github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
Like this:
像这样:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
and use them like this:
并像这样使用它们:
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
// code here
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
// code here
}
Outdated version below
下面是过时的版本
to get OS version:
获取操作系统版本:
[[UIDevice currentDevice] systemVersion]
returns string, which can be turned into int/float via
返回字符串,可以通过以下方式将其转换为 int/float
-[NSString floatValue]
-[NSString intValue]
like this
像这样
Both values (floatValue, intValue) will be stripped due to its type, 5.0.1 will become 5.0 or 5 (float or int), for comparing precisely, you will have to separate it to array of INTs check accepted answer here: Check iPhone iOS Version
由于其类型,这两个值(floatValue,intValue)都将被剥离,5.0.1 将变为 5.0 或 5(float 或 int),为了精确比较,您必须将其与 INT 数组分开检查接受的答案在这里:检查 iPhone iOS版本
NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];
and compare like this
并像这样比较
NSLog(@"System Version is %@",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;
For Swift 4.0 syntax
对于 Swift 4.0 语法
below example is just checking if the device is of iOS11
or greater version.
下面的例子只是检查设备是否是iOS11
或更高版本。
let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
//"for ios 11"
}
else{
//"ios below 11")
}
回答by Gabriele Petronella
Update
更新
From iOS 8 we can use the new isOperatingSystemAtLeastVersion
method on NSProcessInfo
从 iOS 8 开始,我们可以使用新isOperatingSystemAtLeastVersion
方法NSProcessInfo
NSOperatingSystemVersion ios8_0_1 = (NSOperatingSystemVersion){8, 0, 1};
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_1]) {
// iOS 8.0.1 and above logic
} else {
// iOS 8.0.0 and below logic
}
Beware that this will crash on iOS 7, as the API didn't exist prior to iOS 8. If you're supporting iOS 7 and below, you can safely perform the check with
请注意,这将在 iOS 7 上崩溃,因为在 iOS 8 之前不存在该 API。如果您支持 iOS 7 及更低版本,则可以安全地执行检查
if ([NSProcessInfo instancesRespondToSelector:@selector(isOperatingSystemAtLeastVersion:)]) {
// conditionally check for any version >= iOS 8 using 'isOperatingSystemAtLeastVersion'
} else {
// we're on iOS 7 or below
}
Original answer iOS < 8
原始答案 iOS < 8
For the sake of completeness, here's an alternative approach proposed by Apple itself in the iOS 7 UI Transition Guide, which involves checking the Foundation Framework version.
为了完整起见,这里有一个 Apple 自己在iOS 7 UI Transition Guide 中提出的替代方法,它涉及检查 Foundation Framework 版本。
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
} else {
// Load resources for iOS 7 or later
}
回答by Yi Jiang
I know I am too late to answer this question. I am not sure does my method still working on low iOS versions (< 5.0):
我知道我回答这个问题为时已晚。我不确定我的方法是否仍然适用于低 iOS 版本(< 5.0):
NSString *platform = [UIDevice currentDevice].model;
NSLog(@"[UIDevice currentDevice].model: %@",platform);
NSLog(@"[UIDevice currentDevice].description: %@",[UIDevice currentDevice].description);
NSLog(@"[UIDevice currentDevice].localizedModel: %@",[UIDevice currentDevice].localizedModel);
NSLog(@"[UIDevice currentDevice].name: %@",[UIDevice currentDevice].name);
NSLog(@"[UIDevice currentDevice].systemVersion: %@",[UIDevice currentDevice].systemVersion);
NSLog(@"[UIDevice currentDevice].systemName: %@",[UIDevice currentDevice].systemName);
You can get these results:
您可以获得以下结果:
[UIDevice currentDevice].model: iPhone
[UIDevice currentDevice].description: <UIDevice: 0x1cd75c70>
[UIDevice currentDevice].localizedModel: iPhone
[UIDevice currentDevice].name: Someones-iPhone002
[UIDevice currentDevice].systemVersion: 6.1.3
[UIDevice currentDevice].systemName: iPhone OS
回答by jbat100
[[UIDevice currentDevice] systemVersion]
回答by ArunHyman
[[UIDevice currentDevice] systemVersion];
[[UIDevice currentDevice] systemVersion];
or check the version like
或检查版本
You can get the below Macros from here.
您可以从这里获取以下宏。
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(IOS_VERSION_3_2_0))
{
UIImageView *background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cs_lines_back.png"]] autorelease];
theTableView.backgroundView = background;
}
Hope this helps
希望这可以帮助
回答by Bhavesh Nayi
[[[UIDevice currentDevice] systemVersion] floatValue]
回答by Anton
Marek Sebera's is great most of the time, but if you're like me and find that you need to check the iOS version frequently, you don't want to constantly run a macro in memory because you'll experience a very slight slowdown, especially on older devices.
Marek Sebera 的大部分时间都很棒,但如果你像我一样发现需要经常检查 iOS 版本,你不想经常在内存中运行宏,因为你会遇到非常轻微的减速,尤其是在旧设备上。
Instead, you want to compute the iOS version as a float once and store it somewhere. In my case, I have a GlobalVariables
singleton class that I use to check the iOS version in my code using code like this:
相反,您希望将 iOS 版本作为浮点数计算一次并将其存储在某处。就我而言,我有一个GlobalVariables
单例类,用于使用如下代码检查代码中的 iOS 版本:
if ([GlobalVariables sharedVariables].iOSVersion >= 6.0f) {
// do something if iOS is 6.0 or greater
}
To enable this functionality in your app, use this code (for iOS 5+ using ARC):
要在您的应用中启用此功能,请使用以下代码(对于使用 ARC 的 iOS 5+):
GlobalVariables.h:
全局变量.h:
@interface GlobalVariables : NSObject
@property (nonatomic) CGFloat iOSVersion;
+ (GlobalVariables *)sharedVariables;
@end
GlobalVariables.m:
全局变量.m:
@implementation GlobalVariables
@synthesize iOSVersion;
+ (GlobalVariables *)sharedVariables {
// set up the global variables as a static object
static GlobalVariables *globalVariables = nil;
// check if global variables exist
if (globalVariables == nil) {
// if no, create the global variables class
globalVariables = [[GlobalVariables alloc] init];
// get system version
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
// separate system version by periods
NSArray *systemVersionComponents = [systemVersion componentsSeparatedByString:@"."];
// set ios version
globalVariables.iOSVersion = [[NSString stringWithFormat:@"%01d.%02d%02d", \
systemVersionComponents.count < 1 ? 0 : \
[[systemVersionComponents objectAtIndex:0] integerValue], \
systemVersionComponents.count < 2 ? 0 : \
[[systemVersionComponents objectAtIndex:1] integerValue], \
systemVersionComponents.count < 3 ? 0 : \
[[systemVersionComponents objectAtIndex:2] integerValue] \
] floatValue];
}
// return singleton instance
return globalVariables;
}
@end
Now you're able to easily check the iOS version without running macros constantly. Note in particular how I converted the [[UIDevice currentDevice] systemVersion]
NSString to a CGFloat that is constantly accessible without using any of the improper methods many have already pointed out on this page. My approach assumes the version string is in the format n.nn.nn (allowing for later bits to be missing) and works for iOS5+. In testing, this approach runs much faster than constantly running the macro.
现在,您无需经常运行宏即可轻松检查 iOS 版本。特别注意我是如何将[[UIDevice currentDevice] systemVersion]
NSString转换为 CGFloat 的,该 CGFloat 可以在不使用许多人已经在本页上指出的任何不正确方法的情况下不断访问。我的方法假设版本字符串的格式为 n.nn.nn(允许缺少后面的位)并且适用于 iOS5+。在测试中,这种方法比持续运行宏要快得多。
Hope this helps anyone experiencing the issue I had!
希望这可以帮助遇到我遇到的问题的任何人!
回答by JohnK
To get more specific version number information with major and minor versions separated:
要获得更具体的版本号信息,主要和次要版本分开:
NSString* versionString = [UIDevice currentDevice].systemVersion;
NSArray* vN = [versionString componentsSeparatedByString:@"."];
The array vN
will contain the major and minor versions as strings, but if you want to do comparisons, version numbers should be stored as numbers(ints). You can add this code to store them in the C-array* versionNumbers
:
该数组vN
将包含主要版本和次要版本作为字符串,但如果要进行比较,版本号应存储为数字(整数)。您可以添加此代码以将它们存储在 C-array* 中versionNumbers
:
int versionNumbers[vN.count];
for (int i = 0; i < sizeof(versionNumbers)/sizeof(versionNumbers[0]); i++)
versionNumbers[i] = [[vN objectAtIndex:i] integerValue];
* C-arrays used here for more concise syntax.
* 此处使用 C 数组以获得更简洁的语法。
回答by callisto
In MonoTouch:
在 MonoTouch 中:
To get the Major version use:
要获得主要版本,请使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
For minor version use:
对于次要版本使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
回答by iCode
A simple check for iOS version less than 5 (all versions):
对低于 5 的 iOS 版本(所有版本)的简单检查:
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
// do something
};