2011-01-31 39 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) 

希望它能帮助。

相关问题