iOS hook原理

OC中的method其实是一个结构体

struct objc_method{

SEL method_name

char *method_types

IMP method_imp

}

SEL是方法名,IMP其实就是一个C函数的指针,可以直接强制转换的,runtime中的IMP可以直接操作这些函数

方法调用流程:Objective-C 消息转发都会转发为objc_msgSend(receiver,@selector(message))

iOS 设备流畅基于runtime和runloop,高效的消息转发机制

寻找IMP的过程():在当前class方法缓存(cache methodLists)里寻找,再去当前类的方法列表(methodLists)里查找,找到后

添加到缓存列表里,没有继续往下执行,再去父类的缓存列表和方法列表里查找,直到找到基类为止

消息转发过程:

1.+(BOOL)resolveInstanceMethod:(SEL)sel和+(BOOL)resolveClassMethod:(SEL)sel;

2.-(id)forwardingTargetForSelector:(SEL)sel

3.-(void)forwardInvocation:(NSInvocation *)anInvocation

若3次拯救机会返回NO,就会抛出异常或者崩溃

猜你喜欢

转载自www.cnblogs.com/tryFighting/p/9156546.html