This question already has an answer here:
这个问题已经有了答案:
- How to check iOS version? 38 answers
- 如何检查iOS版本?38个答案
I want to check if the user is running the app on iOS less than 5.0 and display a label in the app.
我想检查一下用户是否在iOS上运行这个应用程序,并在app中显示一个标签。
How do I detect which iOS is running on user's device programmatically?
如何通过编程的方式检测用户设备上运行的iOS ?
Thanks!
谢谢!
10 个解决方案
#1
653
Best current version, without need to deal with numeric search within NSString is to define macros
(See original answer: Check iPhone iOS Version)
最好的当前版本,不需要在NSString中处理数字搜索就可以定义宏(参见原始答案:检查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),为了比较精确,您将不得不将它与INTs的数组进行分离,在这里检查已接受的答案:检查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
斯威夫特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")
}
#2
251
Update
From iOS 8 we can use the new isOperatingSystemAtLeastVersion
method on NSProcessInfo
在ios8中,我们可以使用新的isoperatingsystematlowversion方法来处理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并不存在。如果您支持ios7和以下,您可以安全地执行检查。
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
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.
为了完整起见,这里是苹果公司在ios7 UI转换指南中提出的另一种方法,它涉及检查基础框架版本。
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
} else {
// Load resources for iOS 7 or later
}
#3
22
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
#4
14
[[UIDevice currentDevice] systemVersion]
#5
14
[[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
希望这有助于
#6
8
[[[UIDevice currentDevice] systemVersion] floatValue]
#7
5
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版本作为一个浮点数进行计算,并将其存储在某个地方。在我的例子中,我有一个全局变量单例类,用来在代码中使用如下代码检查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):
要在您的应用程序中启用此功能,请使用此代码(针对iOS 5+使用ARC):
GlobalVariables.h:
GlobalVariables.h:
@interface GlobalVariables : NSObject
@property (nonatomic) CGFloat iOSVersion;
+ (GlobalVariables *)sharedVariables;
@end
GlobalVariables.m:
GlobalVariables.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的,它可以在不使用任何已指出的错误方法的情况下不断访问。我的方法假定版本字符串是nn格式的。nn(允许稍后部分丢失)并为iOS5+工作。在测试中,这种方法比持续运行宏要快得多。
Hope this helps anyone experiencing the issue I had!
希望这能帮助任何人遇到我的问题!
#8
2
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将包含主要的和次要的版本作为字符串,但是如果您想做比较,版本号应该存储为数字(ints)。您可以添加此代码以将它们存储在c数组* 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数组用于更简洁的语法。
#9
1
In MonoTouch:
在MonoTouch:
To get the Major version use:
使用主要版本:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
For minor version use:
小版本的使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
#10
0
A simple check for iOS version less than 5 (all versions):
一个简单的检查iOS版本小于5(所有版本):
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
// do something
};
#1
653
Best current version, without need to deal with numeric search within NSString is to define macros
(See original answer: Check iPhone iOS Version)
最好的当前版本,不需要在NSString中处理数字搜索就可以定义宏(参见原始答案:检查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),为了比较精确,您将不得不将它与INTs的数组进行分离,在这里检查已接受的答案:检查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
斯威夫特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")
}
#2
251
Update
From iOS 8 we can use the new isOperatingSystemAtLeastVersion
method on NSProcessInfo
在ios8中,我们可以使用新的isoperatingsystematlowversion方法来处理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并不存在。如果您支持ios7和以下,您可以安全地执行检查。
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
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.
为了完整起见,这里是苹果公司在ios7 UI转换指南中提出的另一种方法,它涉及检查基础框架版本。
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// Load resources for iOS 6.1 or earlier
} else {
// Load resources for iOS 7 or later
}
#3
22
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
#4
14
[[UIDevice currentDevice] systemVersion]
#5
14
[[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
希望这有助于
#6
8
[[[UIDevice currentDevice] systemVersion] floatValue]
#7
5
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版本作为一个浮点数进行计算,并将其存储在某个地方。在我的例子中,我有一个全局变量单例类,用来在代码中使用如下代码检查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):
要在您的应用程序中启用此功能,请使用此代码(针对iOS 5+使用ARC):
GlobalVariables.h:
GlobalVariables.h:
@interface GlobalVariables : NSObject
@property (nonatomic) CGFloat iOSVersion;
+ (GlobalVariables *)sharedVariables;
@end
GlobalVariables.m:
GlobalVariables.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的,它可以在不使用任何已指出的错误方法的情况下不断访问。我的方法假定版本字符串是nn格式的。nn(允许稍后部分丢失)并为iOS5+工作。在测试中,这种方法比持续运行宏要快得多。
Hope this helps anyone experiencing the issue I had!
希望这能帮助任何人遇到我的问题!
#8
2
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将包含主要的和次要的版本作为字符串,但是如果您想做比较,版本号应该存储为数字(ints)。您可以添加此代码以将它们存储在c数组* 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数组用于更简洁的语法。
#9
1
In MonoTouch:
在MonoTouch:
To get the Major version use:
使用主要版本:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
For minor version use:
小版本的使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
#10
0
A simple check for iOS version less than 5 (all versions):
一个简单的检查iOS版本小于5(所有版本):
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
// do something
};