iOS Swizzled 方法的替換、給原方法修改、添加代碼

在iOS的開(kāi)發(fā)當(dāng)中、我們需要對(duì)原有的方法進(jìn)行改進(jìn)時(shí),我們可以通過(guò)重寫(xiě)父類實(shí)現(xiàn)。但是,當(dāng)我們需要在所有的子類都要修改父類方法,如果父類是我們自定義的API當(dāng)然可以直接修改API,通常需要修改系統(tǒng)API活著三方庫(kù),就需要用到runtime時(shí)機(jī)制進(jìn)行方法替換活著補(bǔ)救。
本文章以hook原系統(tǒng)的dealloc方法添加一句NSLog來(lái)打印當(dāng)前頁(yè)面的釋放狀態(tài)

采用category重寫(xiě) load 方法,導(dǎo)入工程即可實(shí)現(xiàn)方法的替換

UIViewController+Swizzled.h

//
//  UIViewController+Swizzled.h
//  FtxBookViaPhone
//
//  Created by Jone on 2017/2/13.
//  Copyright ? 2017年 FTXJOY. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIViewController (Swizzled)

@end

UIViewController+Swizzled.m

//
//  UIViewController+Swizzled.m
//  FtxBookViaPhone
//
//  Created by Jone on 2017/2/13.
//  Copyright ? 2017年 FTXJOY. All rights reserved.
//

#import "UIViewController+Swizzled.h"

@implementation UIViewController (Swizzled)

+(void)load {
    [super load];
    
    __weak typeof(self) weakSelf = self;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [weakSelf class];
        Method oldMethod = class_getInstanceMethod(weakSelf, NSSelectorFromString(@"dealloc"));
        Method newMethod = class_getInstanceMethod(weakSelf, @selector(swizzledDealloc));
        BOOL didAddMethod = class_addMethod(class, NSSelectorFromString(@"dealloc"), method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
        if (didAddMethod) {
            class_replaceMethod(class, @selector(swizzledDealloc), method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod));
        } else {
            method_exchangeImplementations(oldMethod, newMethod);
        }
    });
}

- (void)swizzledDealloc {
    NSLog(@"print:dealloc-%@",NSStringFromClass([self class]));
    
    [self swizzledDealloc];
}

@end

Demo

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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