簡體   English   中英

如何以編程方式獲得正確的iOS版本?

[英]How to get the proper iOS version programmatically?

我想獲得當前用戶設備的操作系統版本,以便在后端進行一些分析。 我想嘗試如下,

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

當我在擁有iOS 8.0.2的iPhone上運行測試時,這個api返回8.000000作為結果,但我需要的是8.0.2的確切iOS版本

提前了解任何有關解決此問題的幫助。

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

[[NSProcessInfo processInfo] operatingSystemVersion]

如果您想檢查特定的API的可用性,再有就是比檢查OS版本,解釋一個更好的辦法在這里

你可以用NSString格式得到它:

[UIDevice currentDevice].systemVersion

新編輯

PS

你改變了你的問題......現在我的回答沒有任何意義了...下次添加一個帶有明顯編輯的新行,讓大家理解問題/答案的主題,請

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

目標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 
}

也許不是更優雅的解決方案,但它確實完成了工作,所以你可以在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

您可以將哪些內容轉換為更適合您的分析格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM