本文翻译自:How can we programmatically detect which iOS version is device running on? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- How to check iOS version? 如何查看iOS版本? 36 answers 36个答案
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! 谢谢!
#1楼
参考:https://stackoom.com/question/wVP0/我们如何以编程方式检测运行设备的iOS版本-重复
#2楼
A simple check for iOS version less than 5 (all versions): 对iOS版本少于5(所有版本)的简单检查:
if([[[UIDevice currentDevice] systemVersion] integerValue] < 5){
// do something
};
#3楼
In MonoTouch: 在MonoTouch中:
To get the Major version use: 要获得主要版本使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[0]
For minor version use: 对于次要版本使用:
UIDevice.CurrentDevice.SystemVersion.Split('.')[1]
#4楼
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. 相反,您希望将iOS版本计算为浮动一次并将其存储在某处。 In my case, I have a GlobalVariables
singleton class that I use to check the iOS version in my code using code like this: 在我的例子中,我有一个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 : 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. 现在,您可以轻松检查iOS版本,而无需经常运行宏。 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. 请特别注意我如何将[[UIDevice currentDevice] systemVersion]
NSString转换为CGFloat,这种CGFloat可以不使用许多已经在本页面指出的任何不正确的方法而不断访问。 My approach assumes the version string is in the format n.nn.nn (allowing for later bits to be missing) and works for iOS5+. 我的方法假设版本字符串的格式为n.nn.nn(允许以后的位丢失)并适用于iOS5 +。 In testing, this approach runs much faster than constantly running the macro. 在测试中,这种方法比不断运行宏的速度快得多。
Hope this helps anyone experiencing the issue I had! 希望这有助于任何人遇到我遇到的问题!
#5楼
[[[UIDevice currentDevice] systemVersion] floatValue]
#6楼
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