Skip to content

Instantly share code, notes, and snippets.

@fahied
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fahied/67909e9f18ed248a7041 to your computer and use it in GitHub Desktop.
Save fahied/67909e9f18ed248a7041 to your computer and use it in GitHub Desktop.
Injecting block in Objective C Methods using RUNTIME
#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