xcode 如何以编程方式获取正确的 iOS 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29346876/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:53:45  来源:igfitidea点击:

How to get the proper iOS version programmatically?

iosobjective-ciphonexcodeipad

提问by Splendor_iOS

I want to get the current os version of user device for some analysis in backend. Im trying to get it as below,

我想获取用户设备的当前操作系统版本,以便在后端进行一些分析。我试图得到它如下,

[[[UIDevice currentDevice] systemVersion] floatValue]; **//not returning the minor version number**

When I test this by running in my iPhone which is having iOS 8.0.2, this api returns me 8.000000 as the result but I need the exact iOS version which is 8.0.2

当我通过在装有 iOS 8.0.2 的 iPhone 上运行来测试这个时,这个 api 返回 8.000000 作为结果,但我需要确切的 iOS 版本,即 8.0.2

Any help in fixing this problem is appreciated in advance.

提前感谢解决此问题的任何帮助。

回答by ZeMoon

In iOS 8 and above, you can use:

在 iOS 8 及更高版本中,您可以使用:

[[NSProcessInfo processInfo] operatingSystemVersion]

If you want to check the availablity of particular API, then there is a better way than checking OS versions, as explained here.

如果您想检查特定的API的可用性,再有就是比检查操作系统版本有更好的方式,如解释在这里

回答by meronix

you can get it with this, in NSString format:

你可以得到它,以 NSString 格式:

[UIDevice currentDevice].systemVersion

NEW EDIT

新编辑

PS

聚苯乙烯

you changed your question... now my answer has no more sense... next time add new lines with an evident edit, to let everyone understand the thread of the question/answers, please

你改变了你的问题......现在我的回答没有更多意义......下次添加带有明显编辑的新行,让每个人都理解问题/答案的线索,请

回答by Mr. Desai

NSString *osVersion = [[UIDevice currentDevice] systemVersion];

回答by stan

Objective C

目标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)

then use like that:

然后像这样使用:

if (SYSTEM_VERSION_LESS_THAN(@"10.0")){
   //your code here 
}

回答by holex

maybe not the more elegant solution ever but it definitely does the job, so you can try something like that in ObjC:

也许不是更优雅的解决方案,但它绝对可以胜任,因此您可以在 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;
}

then:

然后:

NumVersion version = [self systemVersion];
NSLog(@"%d, %d, %d, %d", version.majorRev, version.minorAndBugRev, version.nonRelRev, version.stage);

will print (in my case at the very moment):

将打印(就我而言):

11, 0, 2, 0

what you'd be able convert to a more desirable format for your analytics.

您可以将什么转换为更理想的分析格式。