Created
April 6, 2012 17:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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