Last active
August 29, 2015 14:08
-
-
Save fahied/67909e9f18ed248a7041 to your computer and use it in GitHub Desktop.
Injecting block in Objective C Methods using 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
#import <BILib.h> | |
#import <objc/runtime.h> | |
// customClass.m | |
// under @implementation | |
//override | |
+(void)load | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
// Iterate over the class and all superclasses | |
Class currentClass = [self class]; | |
NSString *className = [NSString stringWithUTF8String:class_getName(currentClass)]; | |
// Iterate over all instance methods for this class | |
unsigned int methodCount; | |
Method *methodList = class_copyMethodList(currentClass, &methodCount); | |
unsigned int i = 0; | |
for (; i < methodCount; i++) { | |
NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(methodList[i]))]; | |
[BILib injectToClassWithName:className methodName:methodName preprocess:^{ | |
assert(className); | |
assert(methodName); | |
NSLog(@"Running %@ '%@'", className,methodName); | |
}]; | |
} | |
free(methodList); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment