Skip to content

Instantly share code, notes, and snippets.

@rpplusplus
Last active August 29, 2015 14:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpplusplus/451b1c231b7a51ce5786 to your computer and use it in GitHub Desktop.
Save rpplusplus/451b1c231b7a51ce5786 to your computer and use it in GitHub Desktop.
Hello World
#import <objc/runtime.h>
#import <objc/message.h>
@implementation AppDelegate
id (*my_msgSend)(id, SEL, ...) = (void *)objc_msgSend;
void (*my_msgSend_)(id, SEL, ...) = (void *)objc_msgSend;;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Class vc = objc_allocateClassPair(NSClassFromString(@"UIViewController"), "ViewController", 0);
SEL selector = sel_registerName("viewDidLoad");
Method origMethod = class_getInstanceMethod(vc,
selector);
IMP origIMP = method_getImplementation(origMethod);
id block = ^(id self){
UIView* v = my_msgSend(self, @selector(view));
UIColor* c = my_msgSend(NSClassFromString(@"UIColor"), @selector(whiteColor));
my_msgSend_(v, @selector(setBackgroundColor:), c);
id label = my_msgSend(NSClassFromString(@"UILabel"), @selector(alloc));
label = my_msgSend(label, @selector(init));
my_msgSend_(label, @selector(setText:), @"Hello World");
my_msgSend_(label, @selector(sizeToFit));
my_msgSend_(label, @selector(setFrame:), CGRectMake(0, 0, 100, 100));
my_msgSend_(v, @selector(addSubview:), label);
};
IMP viewDidLoad = imp_implementationWithBlock(block);
method_setImplementation(origMethod, viewDidLoad);
objc_registerClassPair(vc);
id myInstance = [[NSClassFromString(@"ViewController") alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = myInstance;
[self.window makeKeyAndVisible];
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment