OC和Swift中實(shí)現(xiàn)Method Swizzling

曾經(jīng)在我剛工作的時(shí)候產(chǎn)品提出一個(gè)需求,進(jìn)行頁(yè)面和事件追蹤,當(dāng)時(shí)用的友盟,于是傻乎乎的在每個(gè)ViewControlelr的viewWillAppear和viewDidAppear中都調(diào)用友盟的接口。后來(lái)接觸到Method Swizzling,才了解到這個(gè)基于Runtime的強(qiáng)大的方式。Method Swizzling是一個(gè)非常有用的技巧,可以全局hook,進(jìn)行行為統(tǒng)計(jì)。

Method Swizzling原理

每個(gè)類都有一個(gè)方法列表,存放著selector的名字和方法實(shí)現(xiàn)的映射關(guān)系。IMP有點(diǎn)類似函數(shù)指針,指向具體的Method實(shí)現(xiàn)。
在Objective-C中調(diào)用一個(gè)方法,其實(shí)是向一個(gè)對(duì)象發(fā)送消息,查找消息的唯一依據(jù)是selector的名字。利用Objective-C的動(dòng)態(tài)特性,可以實(shí)現(xiàn)在運(yùn)行時(shí)偷換selector對(duì)應(yīng)的方法實(shí)現(xiàn),達(dá)到給方法掛鉤的目的。

在實(shí)現(xiàn)Method Swizzling時(shí),核心代碼主要就是一個(gè)runtime的C語(yǔ)言API:

OBJC_EXPORT void method_exchangeImplementations(Method m1, Method m2) 
 __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

原來(lái)的Dispatch Table


Screen Shot 2018-04-14 at 4.36.47 PM.png

然后調(diào)用method_exchangeImplementations方法交換后變成的原來(lái)的Dispatch Table


Screen Shot 2018-04-14 at 4.38.47 PM.png

Method Swizzling實(shí)戰(zhàn)

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 有兩個(gè)必要條件:

  1. 包含 swizzle 方法的類需要繼承自 NSObject
  2. 需要 swizzle 的方法必須有動(dòng)態(tài)屬性(dynamic attribute)

Swift3中重寫initialize方法

public override class func initialize() {


}

Swift4后initialize失效了,可以按照下面這種方式實(shí)現(xiàn)

//
//  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函數(shù)保證代碼只被執(zhí)行一次,防止又被交換回去導(dǎo)致得不到想要的效果
        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)
            //在進(jìn)行 Swizzling 的時(shí)候,需要用 class_addMethod 先進(jìn)行判斷一下原有類中是否有要替換方法的實(shí)現(xiàn)
            let didAddMethod: Bool = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
            //如果 class_addMethod 返回 yes,說(shuō)明當(dāng)前類中沒(méi)有要替換方法的實(shí)現(xiàn),所以需要在父類中查找,這時(shí)候就用到 method_getImplemetation 去獲取 class_getInstanceMethod 里面的方法實(shí)現(xiàn),然后再進(jìn)行 class_replaceMethod 來(lái)實(shí)現(xiàn) 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中調(diào)用initializeMethod,因此swizzling applies and stays.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        UIViewController.initializeMethod()

        return true
 }

注意

在Swift中盡量少使用Method Swizzling,畢竟Swift 關(guān)于方法派發(fā)是使用靜態(tài)方法,只有當(dāng)你的問(wèn)題不能用 Swift 的方式解決,也不能用子類、協(xié)議或擴(kuò)展解決時(shí)才使用。不過(guò)聽(tīng)說(shuō)蘋果又想在Swift5中增加Runtime,其實(shí)私心是支持的,Runtime很強(qiáng)大。
個(gè)人建議其實(shí)不光在Swift中盡量少使用Method Swizzling,在OC中也盡量少使用,只有當(dāng)沒(méi)有其他更好的方式解決你的需要時(shí)才用。

參考文章
Swift 4 Method Swizzling (Part 2/2)
如何優(yōu)雅地在Swift4中實(shí)現(xiàn)Method Swizzling
Effective Method Swizzling in Swift
Objective-C的hook方案(一): Method Swizzling

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容