A,B 继承 Base
@interface Base : NSObject
- (void)print:(NSString*)msg;
- (void)hookPrint:(NSString*)msg;
@end
@implementation Base
- (void)print:(NSString*)msg{
NSLog(@"print-->obj %@ print say:%@", NSStringFromClass(self.class), msg);
}
- (void)hookPrint:(NSString*)msg {
NSLog(@"hookPrin-->obj %@ print say:%@", NSStringFromClass(self.class), msg);
}
@end
@interface ATest : Base
- (void)print:(NSString*)msg;
@end
@implementation ATest
- (void)print:(NSString*)msg {
NSLog(@"A obj print say:%@", msg);
}
@end
@interface BTest : Base
@end
@implementation BTest
@end
1.class_replaceMethod
1.1具体替换方法实现
- (void)oneSwizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector
{ Class cls = [self class];
Method originalMethod = class_getInstanceMethod(cls, origSelector);
Method swizzledMethod = class_getInstanceMethod(cls, newSelector);
//class_replaceMethod 返回的IMP是 没有替代覆盖之前的IMP
IMP previousIMP = class_replaceMethod(cls,
origSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
//newSelector 跟 previousIMP(origSelector以前的实现)进行组合,所谓replaceMethod,其实跟像是强制更新覆盖
//newSelector 方法名,previousIMP是实现(IMP)地址(origSelector以前的实现)覆盖掉现有的newSelectorIMP现在的实现
if (previousIMP) {//如果previousIMP(origSelector以前的实现)存在,则用以前的imp 去跟 newSelector 组合
class_replaceMethod(cls,
newSelector,
previousIMP,
method_getTypeEncoding(originalMethod));
}else{//如果previousIMP(origSelector以前的实现)不存在,则用 寻找父类的同名方法imp 跟 newSelector 组合
class_replaceMethod(cls,
newSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
}
}
1.2程序调用
[ATest oneSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)];
[BTest oneSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)];
//测试代码是这样的:
ATest *a1111 = [ATest new];
[a1111 print:@"hello1"];
[a1111 hookPrint:@"hello1"];
BTest *b1111 = [BTest new];
[b1111 print:@"hello2"];
[b1111 hookPrint:@"hello2"];
Base *base1 = [Base new];
[base1 print:@"hello3"];
[base1 hookPrint:@"hello3"];
1.3输出结果
2021-11-27 16:03:54.935814+0800 AddMethodDemo[16240:2459732] hookPrin-->obj ATest print say:hello1
2021-11-27 16:03:54.935986+0800 AddMethodDemo[16240:2459732] A obj print say:hello1
2021-11-27 16:03:54.936117+0800 AddMethodDemo[16240:2459732] hookPrin-->obj BTest print say:hello2
2021-11-27 16:03:54.936295+0800 AddMethodDemo[16240:2459732] print-->obj BTest print say:hello2
2021-11-27 16:03:54.936440+0800 AddMethodDemo[16240:2459732] print-->obj Base print say:hello3
2021-11-27 16:03:54.936561+0800 AddMethodDemo[16240:2459732] hookPrin-->obj Base print say:hello3
class_replaceMethod,方法交换,在当前类 有方法直接换,没有方法,会去父类中找,如果有方法名没有IMP就会报野指针,但不会影响父类。
用class_replaceMethod()实现的。由于A中不存在hookPrint方法,class_replaceMethod会调用class_addMethod方法,而class_addMethod会把Base的hookPrint实现添加到当前类,print的实现最终会和hookPrint的实现交换。B中俩方法都不存在,也会添加俩方法,最终交换俩方法的实现。最终打印的时候,会调用Base的hookPrint方法。
2. method_exchangeImplementations
2.1具体替换方法实现
- (void)twoSwizzleInstanceMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
Class cls = [self class];
Method originalMethod = class_getInstanceMethod(cls, origSelector);
Method swizzledMethod = class_getInstanceMethod(cls, newSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
}
2.2程序调用
[ATest twoSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)];
[BTest twoSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)];
//测试代码是这样的:
ATest* a = [ATest new]; [a print:@"hello1"];
BTest* b = [BTest new]; [b print:@"hello2"];
Base *base2 = [Base new];
[base2 print:@"hello3"];
2.3输出结果
2021-11-27 16:09:58.219639+0800 AddMethodDemo[16300:2463887] hookPrin-->obj ATest print say:hello1
2021-11-27 16:09:58.219816+0800 AddMethodDemo[16300:2463887] A obj print say:hello2
2021-11-27 16:09:58.219943+0800 AddMethodDemo[16300:2463887] A obj print say:hello3
method_exchangeImplementations,子类有存在交换方法的,优先,子类没有去父类中找,直接交换IMP很暴力。
用method_exchangeImplementations实现。由于A没有实现hookPrint方法,在调用
[A twoSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)]的时候,将A的print实现与Base的hookPrint实现交换了。
接着调用 [B twoSwizzleInstanceMethod:@selector(print:) withMethod:@selector(hookPrint:)]的时候,由于B类啥都没实现,它只能将Base的print实现与base的hookPrint交换了。最终,调用[b print:@"hello2"]的时候,调用的是代码中A类的print方法的实现
地址在项目草稿中。。。