核心代码就是下面这些,照着弄到你自己的工程里就能用。原理上边的文章讲得很优秀了,速度去点个赞。这里介绍下使用时需要注意的点:
-
#import <objc/runtime.h>
别忘了 - 类方法和实例方法是有区别的,代码中的注释有说明
- 使用时需要引入分类,不然是不会生效的
- 我的代码里[NSThread callStackSymbols]是打印调用栈,请根据需要调整。
#import "LoadingToast+test.h"
#import <objc/runtime.h>
@implementation LoadingToast (test)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Class class = [self class];
// When swizzling a class method, use the following:
Class class = object_getClass((id)self);
SEL originalSelector = @selector(hideToastActivity);
SEL swizzledSelector = @selector(track_hideToastActivity);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
+ (void)track_hideToastActivity {
[self track_hideToastActivity];
NSLog(@"track_hideToastActivity : %@",[NSThread callStackSymbols]);
}
@end
网友评论