简体   繁体   中英

How to programmatically get iOS's alphanumeric version string

I've been working with the nice PLCrashReport framework to send to my server the crash reports from my user's iOS devices.

However, to symbolicate the crash report, the utility requests that along with the iPhone OS version number, I have the ipsw's alphanumeric version, in the form of:实用程序要求我提供 ipsw 的字母数字版本以及 iPhone 操作系统版本号,格式为:

OS Version:      iPhone OS 4.0.1 (8A293)

I know that I can get the iOS's numeric version by [[UIDevice currentDevice] systemVersion] , but how can I get the other one?

I can't find a way, and I've searched everywhere I could imagine.

Not sure why others are saying that this is not possible because it is using the sysctl function.

#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;   
}

Why don´t you try this?

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

NSLog(@"%@", os_version);

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

I've reached here looking for an answer to how to do this in Swift and, after some test and error, just found that you can write this, at least in Xcode 9:

print(ProcessInfo().operatingSystemVersionString)

And the output I get in simulator is:

Version 11.0 (Build 15A5278f)

And in a real device:

Version 10.3.2 (Build 14F89)

Hope it helps.

I had problems uploading Dylan's string to a PHP web server, the URL connection would just hang, so I modified the code as follows to fix it:

#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;  
    }

There is no API for this (at least, not in UIKit). Please file a bug requesting it.

“另一个”是构建版本,您的设备无法通过 UIKit 使用。

Swift version of @Dylan Copeland answer.

func systemBuild() -> String? {
    var mib: [Int32] = [CTL_KERN, KERN_OSVERSION]
    let namelen = u_int(MemoryLayout.size(ofValue: mib) / MemoryLayout.size(ofValue: mib[0]))
    var bufferSize: size_t = 0
    
    // Get the size for the buffer
    sysctl(&mib, namelen, nil, &bufferSize, nil, 0)
    
    var buildBuffer: [u_char] = .init(repeating: 0, count: bufferSize)
    
    let result = sysctl(&mib, namelen, &buildBuffer, &bufferSize, nil, 0)
    
    if result >= 0 && bufferSize > 0 {
        return String(bytesNoCopy: &buildBuffer, length: bufferSize - 1, encoding: .utf8, freeWhenDone: false)
    }
    
    return nil
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM