2011-01-31 41 views
19

我一直在使用nice框架向我的服務器發送來自我的用戶的iOS設備的崩潰報告。如何以編程方式獲取iOS的字母數字版本字符串

然而,symbolicate崩潰報告中,symbolicatecrash實用的請求,與iPhone OS版本號一起,我有IPSW的字母數字版本,在形式:

OS Version:  iPhone OS 4.0.1 (8A293) 

我知道我可以通過[[UIDevice currentDevice] systemVersion]獲得iOS的數字版本,但是如何獲得另一個?

我找不到方法,我在任何地方都可以想象。

+2

爲了記錄,該字符串是內部版本號。 – Chuck 2011-02-03 22:19:04

回答

27

不確定爲什麼其他人說這是不可能的,因爲它使用sysctl函數。

#import <sys/sysctl.h>  

- (NSString *)osVersionBuild { 
    int mib[2] = {CTL_KERN, KERN_OSVERSION}; 
    u_int namelen = sizeof(mib)/sizeof(mib[0]); 
    size_t bufferSize = 0; 

    NSString *osBuildVersion = nil; 

    // Get the size for the buffer 
    sysctl(mib, namelen, NULL, &bufferSize, NULL, 0); 

    u_char buildBuffer[bufferSize]; 
    int result = sysctl(mib, namelen, buildBuffer, &bufferSize, NULL, 0); 

    if (result >= 0) { 
     osBuildVersion = [[[NSString alloc] initWithBytes:buildBuffer length:bufferSize encoding:NSUTF8StringEncoding] autorelease]; 
    } 

    return osBuildVersion; 
} 
+0

Dylan,您在iOS中使用CTL_KERN和KERN_OSVERSION的值是什麼? – 2011-03-29 00:17:35

+0

這兩個值在`sys/sysctl.h`中定義。 – 2011-03-29 06:17:00

+1

+1非常酷。 :) – 2011-04-04 23:32:04

1

這沒有API(至少,不在UIKit中)。請file a bug請求它。

0

'其他'是構建版本,並且不通過UIKit可用於您的設備。

8

你爲什麼不試試這個?

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

NSLog(@"%@", os_version); 

if([[NSNumber numberWithChar:[os_version characterAtIndex:0]] intValue]>=4) { 
    // ... 
} 
3

我有問題上傳Dylan的字符串到PHP的Web服務器,URL連接將只是掛,所以我修改了代碼如下解決它:

#include <sys/sysctl.h> 

    - (NSString *)osVersionBuild { 
     int mib[2] = {CTL_KERN, KERN_OSVERSION}; 
     size_t size = 0; 

     // Get the size for the buffer 
     sysctl(mib, 2, NULL, &size, NULL, 0); 

     char *answer = malloc(size); 
     int result = sysctl(mib, 2, answer, &size, NULL, 0); 

     NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding]; 
     free(answer); 
     return results; 
    } 
1

我在這裏達到期待的答案,如何做到這一點的雨燕,一些測試和錯誤之後,才發現,你可以這樣寫,至少在Xcode 9:

print(ProcessInfo().operatingSystemVersionString) 

和輸出我模擬器得到的是:

Version 11.0 (Build 15A5278f) 

而在真實的設備:

Version 10.3.2 (Build 14F89) 

希望它能幫助。

相關問題