话不多说直接上代码,欢迎共同探讨!
+ (BOOL)swizzle:(SEL)original with:(IMP)replacement store:(IMPPointer)store {
return class_swizzleMethodAndStore(self, original, replacement, store);
}
BOOL class_swizzleMethodAndStore(Class class, SEL original, IMP replacement, IMPPointer store) {
IMP imp = NULL;
Method method = class_getInstanceMethod(class, original);
if (method) {
const char *type = method_getTypeEncoding(method);
imp = class_replaceMethod(class, original, replacement, type);
if (!imp) {
imp = method_getImplementation(method);
}
}
if (imp && store) { *store = imp; }
return (imp != NULL);
}
程序入口
+(void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self swizzle:@selector(viewWillAppear:) with:(IMP)xxx_viewWillAppear store:(IMP *)&Setxxx_viewWillAppearIMP];
});
}
static void xxx_viewWillAppear(id self,SEL _cmd,BOOL animated);
static void (*Setxxx_viewWillAppearIMP)(id self, SEL _cmd, BOOL animated);
static void xxx_viewWillAppear(id self, SEL _cmd, BOOL animated) {
Setxxx_viewWillAppearIMP(self, _cmd, animated);
NSLog(@"viewWillAppear = %s",__func__);
}
网友评论