How to programmatically get iOS's alphanumeric version string
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 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;
}