Created
February 6, 2011 21:55
-
-
Save mlen/813756 to your computer and use it in GitHub Desktop.
using objc from c
This file contains hidden or 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
/* | |
* Compile with: | |
* gcc -g -Wall -pedantic -framework Foundation -framework ScriptingBridge -o objc-test objc-test.c | |
*/ | |
#include <objc/objc-runtime.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
void listClasses() { | |
int i, num; | |
Class *classes; | |
num = objc_getClassList(NULL, 0); | |
if (num > 0) { | |
classes = malloc(sizeof(Class) * num); | |
num = objc_getClassList(classes, num); | |
for (i = 0; i < num; i++) { | |
printf("%d:\t%s\n", i+1, class_getName(classes[i])); | |
} | |
free(classes); | |
} | |
} | |
void listMethods(id obj) { | |
unsigned i, num; | |
Method *methods; | |
if ((methods = class_copyMethodList((Class)object_getClass(obj), &num))) { | |
for (i = 0; i < num; i++) { | |
printf("%d:\t%s\n", i+1, sel_getName(method_getName(methods[i]))); | |
} | |
free(methods); | |
} | |
} | |
id NSAutoreleasePoolCreate() { | |
id NSAutoreleasePool = objc_getClass("NSAutoreleasePool"); | |
return objc_msgSend(NSAutoreleasePool, sel_registerName("new")); | |
} | |
void NSAutoreleasePoolDrain(id pool) { | |
objc_msgSend(pool, sel_registerName("drain")); | |
} | |
int main() { | |
id pool = NSAutoreleasePoolCreate(); | |
id SBApplication = objc_getClass("SBApplication"); | |
id NSString = objc_getClass("NSString"); | |
id myString = objc_msgSend(NSString, sel_registerName("stringWithCString:"), "com.apple.iTunes"); | |
id myApp = objc_msgSend(SBApplication, sel_registerName("applicationWithBundleIdentifier:"), myString); | |
printf("List of methods of iTunesApplication\n\n"); | |
listMethods(myApp); | |
printf("\n\nList of classes\n\n"); | |
listClasses(); | |
objc_msgSend(myApp, sel_registerName("playpause")); | |
NSAutoreleasePoolDrain(pool); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment