Skip to content

Instantly share code, notes, and snippets.

@deltheil
Created April 6, 2012 17:09
Show Gist options
  • Save deltheil/2321409 to your computer and use it in GitHub Desktop.
Save deltheil/2321409 to your computer and use it in GitHub Desktop.
iOS: get device OS version from C code via the Objective-C Runtime
#include <objc/runtime.h>
#include <objc/message.h>
#include <CoreFoundation/CoreFoundation.h>
/* ... */
/**
* Return a character string that holds the current version
* of the operating system which is equivalent to:
* `[[UIDevice currentDevice] systemVersion]`
* in plain Obj-C code.
* The caller must manage deletion.
*/
char *getsystemversion(void) {
char *sv = NULL;
id Dev = objc_msgSend(objc_getClass("UIDevice"), sel_getUid("currentDevice"));
CFStringRef SysVer = (CFStringRef) objc_msgSend(Dev, sel_getUid("systemVersion"));
CFIndex len = CFStringGetLength(SysVer);
CFIndex max = CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8);
sv = (char *) malloc(max + 1);
CFStringGetCString(SysVer, sv, max, kCFStringEncodingUTF8);
return sv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment