直接进入正题~
相信大家应该都见过Method Swizzling的标准模板,举个 一缕殇流化隐半边冰霜 大神文中的例子:
@implementation UIViewController (Swizzling)
+ (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(viewWillAppear:);
SEL swizzledSelector = @selector(xxx_viewWillAppear:);
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);
}
});
}
这里提到一句话
一般我们使用都是新建一个分类,在分类中进行Method Swizzling方法的交换。
可是如果我并不是在新建的Category 中,那么可能就要换一种写法了。
首先来分析一下上面获取Method的两句代码
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
由于一般都是新建Category,所以这里的class其实就是分类自己的class,而且由于是Category,所以UIViewController也会拿到swizzledSelector,那么这两个Method都可以拿到。但是,我现在如果新建了一个和UIViewController 无关的targetClass类,那么UIViewController就拿不到swizzledSelector,进一步swizzledMethod也就变成了nil。这就坑爹了……
那么想一想该怎么解决这个问题。既然UIViewController拿不到,那我就把这个selector加给你好了。用下面的方法:
Method targetMethod = class_getInstanceMethod(targetClass, targetSelector);
IMP targetIMP = class_getMethodImplementation(targetClass ,targetSelector);
BOOL didAddMethod = class_addMethod(originalClass, targetSelector, targetIMP, method_getTypeEncoding(targetMethod));
注意这里和常见模板的不同之处,常见模板class_addMethod是这样的:
BOOL didAddMethod=class_addMethod(class
,originalSelector
,method_getImplementation(swizzledMethod)
,method_getTypeEncoding(swizzledMethod));
而现在要做的是不但把Method加过去,还要把targetSeletor加过去。而且后续的可是这样做又带来一个问题是,如果我要把这个模板函数定义成一个通用方法,让所有类都能调用的话,就要区分是否Category。而且还有一个问题是,为了确保方法只被swizzling一次,一般都在load函数里面写个dispatch_once。但是如果漏写了呢?我想要的是通用方法啊。
下面给出的是我摸索出来的模板:
NS_INLINE void ETrackingHookingMethod(Class originalClass, Class targetClass, SEL originalSelector){
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
SEL targetSelector = NSSelectorFromString([NSString stringWithFormat:@"etracking_%@", NSStringFromSelector( originalSelector)]);
#pragma clang diagnostic pop
static NSMutableSet * allHookedClass = nil;
if (allHookedClass == nil) {
allHookedClass = [NSMutableSet set];
}
if (!originalClass){
return;
}else{
NSString *hookedSelector = [NSString stringWithFormat:@"%@_%@",NSStringFromClass(originalClass),NSStringFromSelector(originalSelector)];
if (![allHookedClass containsObject:hookedSelector]) {
[allHookedClass addObject:hookedSelector];
}else{
return;
}
}
NSLog(@"allhookClass%@",allHookedClass);
Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
Method targetMethod = class_getInstanceMethod(targetClass, targetSelector);
if ((!targetMethod) || (!originalMethod)) {
return;
}
Method hasTargetMethod = class_getInstanceMethod(originalClass, targetSelector);
IMP targetIMP = class_getMethodImplementation(targetClass ,targetSelector);
BOOL didAddMethod = NO;
if (originalClass == targetClass) {
didAddMethod = class_addMethod(originalClass,originalSelector,method_getImplementation(targetMethod),method_getTypeEncoding(targetMethod));
if (didAddMethod) {
IMP replacedMethod = class_replaceMethod(originalClass,
targetSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, targetMethod);
}
}else{
if (hasTargetMethod) {
return;
}else{
didAddMethod = class_addMethod(originalClass, targetSelector, targetIMP, method_getTypeEncoding(targetMethod));
Method originTargetMethod = class_getInstanceMethod(originalClass, targetSelector);
BOOL hasSource = class_respondsToSelector(originalClass, originalSelector);
BOOL hasTarget = class_respondsToSelector(targetClass, targetSelector);
if (hasSource && hasTarget) {
method_exchangeImplementations(originalMethod,originTargetMethod);
}
}
}
}
关于hook UITableViewDelegate 的,可以新建一个类,在load方法里面调用
+(void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ETrackingHookingMethod([UITableView class], [ETrackingTableViewExplorer class], @selector(setDelegate:));
});
}
先hook到setDelegate:方法,然后在自己类中这么处理(这里比较猥琐了):
-(void)etracking_setDelegate:(id)delegate{
Class conformClass = [delegate class];
while ([conformClass conformsToProtocol:@protocol(UITableViewDelegate)]) {
Class superConformClass = class_getSuperclass(conformClass);
if ([superConformClass conformsToProtocol:@protocol(UITableViewDelegate)]) {
conformClass = superConformClass;
}else{
break;
}
}
ETrackingHookingMethod(conformClass,[ETrackingTableViewExplorer class],@selector(tableView:didSelectRowAtIndexPath:));
ETrackingHookingMethod(conformClass,[ETrackingTableViewExplorer class],@selector(tableView:cellForRowAtIndexPath:));
[self etracking_setDelegate:delegate];
}
这样你就可以随意新增想要hook的方法了。同理这样可以处理自己类中的appDelegate,collectionDelegate等等一系列方法。
参考文章:
南峰子的技术博客
一缕殇流化隐半边冰霜