How to programmatically get iOS's alphanumeric version string

前端 未结 6 642
离开以前
离开以前 2020-12-05 14:20

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

相关标签:
6条回答
  • 2020-12-05 14:45

    Why don´t you try this?

    NSString *os_version = [[UIDevice currentDevice] systemVersion];
    
    NSLog(@"%@", os_version);
    
    if([[NSNumber numberWithChar:[os_version characterAtIndex:0]] intValue]>=4) {
        // ...
    }
    
    0 讨论(0)
  • 2020-12-05 14:45

    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;  
        }
    
    0 讨论(0)
  • 2020-12-05 14:48

    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;   
    }
    
    0 讨论(0)
  • 2020-12-05 14:52

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

    0 讨论(0)
  • 2020-12-05 14:54

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

    0 讨论(0)
  • 2020-12-05 15:05

    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.

    0 讨论(0)
提交回复
热议问题