OC和Swift中实现Method Swizzling
曾经在我刚工作的时候产品提出一个需求,进行页面和事件追踪,当时用的友盟,于是傻乎乎的在每个ViewControlelr的viewWillAppear和viewDidAppear中都调用友盟的接口。后来接触到Method Swizzling,才了解到这个基于Runtime的强大的方式。Method Swizzling是一个非常有用的技巧,可以全局hook,进行行为统计。
Method Swizzling原理
每个类都有一个方法列表,存放着selector的名字和方法实现的映射关系。IMP有点类似函数指针,指向具体的Method实现。
在Objective-C中调用一个方法,其实是向一个对象发送消息,查找消息的唯一依据是selector的名字。利用Objective-C的动态特性,可以实现在运行时偷换selector对应的方法实现,达到给方法挂钩的目的。
在实现Method Swizzling
时,核心代码主要就是一个runtime的C语言API:
OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
原来的Dispatch Table
Screen Shot 2018-04-14 at 4.36.47 PM.png
然后调用method_exchangeImplementations方法交换后变成的原来的Dispatch Table
Screen Shot 2018-04-14 at 4.38.47 PM.png
Method Swizzling实战
OC中使用
#import "UIViewController+swizzling.h"
#import <objc/runtime.h>
@implementation UIViewController (swizzling)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL origSel = @selector(viewDidAppear:);
SEL swizSel = @selector(swiz_viewDidAppear:);
[UIViewController swizzleMethods:[self class] originalSelector:origSel swizzledSelector:swizSel];
}
}
//exchange implementation of two methods
+ (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel
{
Method origMethod = class_getInstanceMethod(class, origSel);
Method swizMethod = class_getInstanceMethod(class, swizSel);
//class_addMethod will fail if original method already exists
BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
if (didAddMethod) {
class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
//origMethod and swizMethod already exist
method_exchangeImplementations(origMethod, swizMethod);
}
}
- (void)swiz_viewDidAppear:(BOOL)animated
{
NSLog(@"I am in - [swiz_viewDidAppear:]");
//handle viewController transistion counting here, before ViewController instance calls its -[viewDidAppear:] method
//需要注入的代码写在此处
[self swiz_viewDidAppear:animated];
}
@end
Swift中是如何使用的呢?
要在 Swift 自定义类中使用 Method Swizzling 有两个必要条件:
- 包含 swizzle 方法的类需要继承自 NSObject
- 需要 swizzle 的方法必须有动态属性(dynamic attribute)
Swift3中重写initialize
方法
public override class func initialize() {
}
Swift4后initialize
失效了,可以按照下面这种方式实现
//
// ViewController+swizzling.swift
// RuntimeDemo
//
// Created by roy.cao on 14/04/2018.
// Copyright © 2018 roy.cao. All rights reserved.
//
import Foundation
import UIKit
private let onceToken = "Method Swizzling"
extension UIViewController {
public class func initializeMethod() {
// Make sure This isn't a subclass of UIViewController, So that It applies to all UIViewController childs
if self != UIViewController.self {
return
}
/**
let _:() = {
}()
*/
//DispatchQueue函数保证代码只被执行一次,防止又被交换回去导致得不到想要的效果
DispatchQueue.once(token: onceToken) {
let originalSelector = #selector(UIViewController.viewWillAppear(_:))
let swizzledSelector = #selector(UIViewController.swizzled_viewWillAppear(animated:))
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
//在进行 Swizzling 的时候,需要用 class_addMethod 先进行判断一下原有类中是否有要替换方法的实现
let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
//如果 class_addMethod 返回 yes,说明当前类中没有要替换方法的实现,所以需要在父类中查找,这时候就用到 method_getImplemetation 去获取 class_getInstanceMethod 里面的方法实现,然后再进行 class_replaceMethod 来实现 Swizzing
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
} else {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
}
}
@objc func swizzled_viewWillAppear(animated: Bool) {
//需要注入的代码写在此处
self.swizzled_viewWillAppear(animated: animated)
}
}
在didFinishLaunchingWithOptions
中调用initializeMethod
,因此swizzling applies and stays.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIViewController.initializeMethod()
return true
}
注意
在Swift中尽量少使用Method Swizzling,毕竟Swift 关于方法派发是使用静态方法,只有当你的问题不能用 Swift 的方式解决,也不能用子类、协议或扩展解决时才使用。不过听说苹果又想在Swift5中增加Runtime,其实私心是支持的,Runtime很强大。
个人建议其实不光在Swift中尽量少使用Method Swizzling,在OC中也尽量少使用,只有当没有其他更好的方式解决你的需要时才用。
参考文章
Swift 4 Method Swizzling (Part 2/2)
如何优雅地在Swift4中实现Method Swizzling
Effective Method Swizzling in Swift
Objective-C的hook方案(一): Method Swizzling