Claudio Martins
Claudio Martins

Reputation: 617

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 symbolicatecrash utility requests that along with the iPhone OS version number, I have the ipsw's alphanumeric version, in the form of:

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.

Upvotes: 23

Views: 13829

Answers (7)

Dmytro Shvetsov
Dmytro Shvetsov

Reputation: 1006

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
}

Upvotes: 0

widemos
widemos

Reputation: 316

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.

Upvotes: 4

Ziggy
Ziggy

Reputation: 91

Why don´t you try this?

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

NSLog(@"%@", os_version);

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

Upvotes: 9

Dylan Copeland
Dylan Copeland

Reputation: 1249

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

Upvotes: 32

malhal
malhal

Reputation: 30617

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

Upvotes: 3

Alan Zeino
Alan Zeino

Reputation: 4396

The 'other one' is the build version, and isn't available to your device through UIKit.

Upvotes: 0

Dave DeLong
Dave DeLong

Reputation: 243156

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

Upvotes: 1

Related Questions