RunTime 之Method Swizzling
2018-03-07 本文已影响0人
進无尽

前言
有关Runtime的知识总结,我本来想集中写成一篇文章的,但是最后发现实在是太长,而且不利于阅读,最后分成了如下几篇:
isa swizzling 的应用
KVO
Method Swizzling API 说明
Method Swizzling技术它能够让我们在运行时替换已有方法来实现我们的一些需求。我们都知道方法由两个部分组成。Selector(SEL)相当于一个方法的id;IMP是方法的实现。这样分开的一个便利之处是selector和IMP之间的对应关系可以被改变。这也是方法拦截替换的实现基础。而 Method Swizzling 可以交换两个方法的实现。
Objective-C 提供了以下 API 来动态替换类方法或实例方法的实现:
-
class_replaceMethod
替换类方法的定义 -
method_exchangeImplementations
交换 2 个方法的实现 -
method_setImplementation
设置 1 个方法的实现
这 3 个方法有一些细微的差别,给大家介绍如下:
-
class_replaceMethod
在苹果的文档(如下图所示)中能看到,它有两种不同的行为。当类中没有想替换的原方法时,该方法会调用class_addMethod
来为该类增加一个新方法,也因为如此,class_replaceMethod
在调用时需要传入types
参数,而method_exchangeImplementations
和method_setImplementation
却不需要。

-
method_exchangeImplementations
的内部实现相当于调用了 2 次method_setImplementation
方法,从苹果的文档中能清晰地了解到(如下图所示)

从以上的区别我们可以总结出这 3 个 API 的使用场景:
-
class_replaceMethod
, 当需要替换的方法可能有不存在的情况时,可以考虑使用该方法。 -
method_exchangeImplementations
,当需要交换 2 个方法的实现时使用。 -
method_setImplementation
最简单的用法,当仅仅需要为一个方法设置其实现方式时使用。
以上 3 个方法的源码在 这里,感兴趣的同学可以读一读。
应用一:拦截系统自带的方法调用Method Swizzling
一般是在load方法中进行,确保最先被调用。+load方法会在Appdelegate的方法之前执行,是最先执行的方法。
使用场景
Method Swizzling 可以重写某个方法而不用继承,同时还可以调用原先的实现。通常的做法是在category中添加一个方法(当然也可以是一个全新的class)。可以通过method_exchangeImplementations这个运行时方法来交换实现。

以下的代码在UIViewController的类别中
+ (void)load {
//在debug模式下进行方法的交换
#ifdef DEBUG
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//原本的viewWillAppear方法
Method viewWillAppear11= class_getInstanceMethod([self class], @selector(viewWillAppear:));
//需要替换成 能够输出日志的viewWillAppear
Method logViewWillAppear11 = class_getInstanceMethod([self class], @selector(logViewWillAppear:));
BOOL addSucc = class_addMethod([self class], @selector(viewWillAppear:), method_getImplementation(logViewWillAppear11), method_getTypeEncoding(logViewWillAppear11));
if (addSucc) {
class_replaceMethod([self class], @selector(logViewWillAppear:), method_getImplementation(viewWillAppear11), method_getTypeEncoding(viewWillAppear11));
}
else{
method_exchangeImplementations(viewWillAppear11, logViewWillAppear11);
}
});
#endif
}
- (void)logViewWillAppear:(BOOL)animated {
NSString *className = NSStringFromClass([self class]);
//在这里,你可以进行过滤操作,指定哪些viewController需要打印,哪些不需要打印
if ([className hasPrefix:@"UI"] == NO) {
NSLog(@"%@ will appear OOOOOO",className);
}
// //下面方法的调用,其实是调用viewWillAppear
// [self logViewWillAppear:animated];
}
-
method_setImplementation 可以让我们提供一个新的函数来代替我们要替换的方法。 而不是将两个方法的实现做交换。
SKPayment *payment = [[SKPayment alloc] init]; Method originalQuantity = class_getInstanceMethod([payment class], @selector(quantity)); method_setImplementation(originalQuantity, (IMP) my_quantity);
应用二:运用Runtime知识替换系统方法避免崩溃
为一些容易造成崩溃的类,在其分类中的 +(void) load方法中完成方法的替换
在自定义的方法中处理掉那些造成崩溃的逻辑即可。

举个栗子:
@implementation NSArray (Safe)
+ (void)load
{
[NSClassFromString(@"__NSArray0") swapMethod:@selector(objectAtIndex:)
currentMethod:@selector(ls_zeroObjectAtIndex:)];
}
- (id)ls_zeroObjectAtIndex:(NSUInteger)index
{
if (index >= self.count)
{
return nil;
}
return [self ls_zeroObjectAtIndex:index];
}