Skip to content

Instantly share code, notes, and snippets.

@grp
Created February 17, 2014 19:05
Show Gist options
  • Save grp/9056864 to your computer and use it in GitHub Desktop.
Save grp/9056864 to your computer and use it in GitHub Desktop.
#import <objc/runtime.h>
#define HookMethod(cls_, sel_, ...) \
(^{ \
SEL _cmd = sel_; \
__block IMP __original = method_setImplementation(class_getInstanceMethod(cls_, _cmd), imp_implementationWithBlock(__VA_ARGS__)); \
})()
#define CallOriginal(...) \
__original(self, _cmd, __VA_ARGS__)
#import <Foundation/Foundation.h>
#import "Hook.h"
@interface Example : NSObject
@end
@implementation Example
- (NSString *)example {
return @"one";
}
@end
void main() {
@autoreleasepool {
HookMethod([Example class], @selector(example), ^(Example *self) {
return [CallOriginal() stringByAppendingString:@" two"];
});
Example *e = [[Example alloc] init];
NSLog(@"example: %@", [e example]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment