【问题标题】:How to get the proper iOS version programmatically?如何以编程方式获取正确的 iOS 版本?
【发布时间】:2015-03-30 12:58:04
【问题描述】:
我想获取用户设备的当前操作系统版本,以便在后端进行一些分析。我试图得到它如下,
[[[UIDevice currentDevice] systemVersion] floatValue]; **//not returning the minor version number**
当我在运行 iOS 8.0.2 的 iPhone 上运行测试时,这个 api 会返回 8.000000 作为结果,但我需要确切的 iOS 版本,即 8.0.2
提前感谢任何解决此问题的帮助。
【问题讨论】:
标签:
ios
objective-c
iphone
xcode
ipad
【解决方案1】:
在 iOS 8 及更高版本中,您可以使用:
[[NSProcessInfo processInfo] operatingSystemVersion]
如果您想检查特定 API 的可用性,那么有比检查操作系统版本更好的方法,如 here 所述。
【解决方案2】:
你可以用这个得到它,NSString 格式:
[UIDevice currentDevice].systemVersion
新编辑
PS
你改变了你的问题……现在我的回答没有意义了……
下次添加带有明显编辑的新行,让每个人都了解问题/答案的线索,
请
【解决方案3】:
目标 C
// define macro
#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)
然后这样使用:
if (SYSTEM_VERSION_LESS_THAN(@"10.0")){
//your code here
}
【解决方案4】:
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
【讨论】:
-
感谢您提供此代码 sn-p,它可能会提供一些即时帮助。正确的解释将greatly improve 其教育价值通过说明为什么这是解决问题的好方法,并使其对未来有类似但不相同的问题的读者更有用。请编辑您的答案以添加解释,并说明适用的限制和假设。
【解决方案5】:
也许不是更优雅的解决方案,但它确实可以完成这项工作,因此您可以在 ObjC 中尝试类似的方法:
- (NumVersion)systemVersion {
NSArray *_separated = [[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."];
NumVersion _version = { 0, 0, 0, 0 };
if (_separated.count > 3) _version.stage = [[_separated objectAtIndex:3] integerValue];
if (_separated.count > 2) _version.nonRelRev = [[_separated objectAtIndex:2] integerValue];
if (_separated.count > 1) _version.minorAndBugRev = [[_separated objectAtIndex:1] integerValue];
if (_separated.count > 0) _version.majorRev = [[_separated objectAtIndex:0] integerValue];
return _version;
}
然后:
NumVersion version = [self systemVersion];
NSLog(@"%d, %d, %d, %d", version.majorRev, version.minorAndBugRev, version.nonRelRev, version.stage);
将打印(以我为例):
11, 0, 2, 0
您可以将其转换为更理想的分析格式。