Created
February 17, 2014 19:05
-
-
Save grp/9056864 to your computer and use it in GitHub Desktop.
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
#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__) |
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
#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