runtime應(yīng)用之crash防護(hù)

  • 當(dāng)我們調(diào)用一個(gè)實(shí)例或者類本身沒(méi)有實(shí)現(xiàn)的方法的時(shí)候會(huì)發(fā)生一個(gè)經(jīng)典的crash,unrecognized selector sent to instance。學(xué)習(xí)過(guò)runtime的知識(shí)后我們知道,利用runtimeAPI去hook系統(tǒng)的方法然后添加自己的操作,了解消息機(jī)制和消息轉(zhuǎn)發(fā)的流程后就可以自己做一些消息轉(zhuǎn)發(fā)。

  • 標(biāo)題所說(shuō)的防護(hù)其實(shí)就是unrecognized selector sent to instance的防護(hù),我利用消息轉(zhuǎn)發(fā)的第二步備用消息接受者,轉(zhuǎn)發(fā)給自己創(chuàng)建的一個(gè)專門處理未知消息的管理類,這樣就可以達(dá)到即使調(diào)用了未知方法也不會(huì)發(fā)生crash的效果。

整個(gè)工具包含一個(gè)類和兩個(gè)分類,沒(méi)有侵入性,通過(guò)hook NSObject類的消息轉(zhuǎn)發(fā)的方法統(tǒng)一把未知消息轉(zhuǎn)發(fā)給自己創(chuàng)建的管理類,然后在管理類中處理未知消息(打印信息或者斷言)。
類結(jié)構(gòu)如下
1,NSObject+ExchangeMethodIMP 提供交換方法的分類,這樣所有的對(duì)象都可以調(diào)用交換方法的API

NSObject+ExchangeMethodIMP.h
@interface NSObject (ExchangeMethodIMP)

+ (void)exchangeMethodWithClass: (Class)currentClass OriginalSel: (SEL)originalSel SwizzlingSel: (SEL)swizzlingSel IsClassMethod: (BOOL)isClassMethod;
@end

2,hook消息轉(zhuǎn)發(fā)的分類 NSObject+MessageFarword
該分類的作用就是hook所有對(duì)象的消息轉(zhuǎn)發(fā)的方法(具體包括-forwardingTargetForSelector:和+forwardingTargetForSelector:)然后統(tǒng)一把消息轉(zhuǎn)發(fā)給自己創(chuàng)建的管理對(duì)象(單例UnrecognizedMessageManager),統(tǒng)一在管理對(duì)象中處理
NSObject+MessageFarword的.m文件如下

BOOL MessageFarwordIsSystemClass(id self){
    NSString *className = NSStringFromClass([self class]);
    if ([className hasPrefix:@"NS"] || [className hasPrefix:@"_"]) {
        return YES;
    }else{
        return NO;
    }
}

@implementation NSObject (MessageFarword)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        // 交換方法 區(qū)分 實(shí)例方法   類方法
        Class class = [NSObject class];
        SEL originalSel = @selector(forwardingTargetForSelector:);
        SEL swizzlingSel = @selector(jt_forwardingTargetForSelector:);
       
        [self exchangeMethodWithClass:class OriginalSel:originalSel SwizzlingSel:swizzlingSel IsClassMethod:YES];
        
        [self exchangeMethodWithClass:class OriginalSel:originalSel SwizzlingSel:swizzlingSel IsClassMethod:NO];
        
    });
}


// hook系統(tǒng)的方法
- (id)jt_forwardingTargetForSelector:(SEL)aSelector{
    
    id asdf = [self jt_forwardingTargetForSelector:aSelector];
    
    if (MessageFarwordIsSystemClass(self)) {
        return asdf;
    }
    // 判斷類如果有自定義消息轉(zhuǎn)發(fā) 就不在處理
    if (asdf == nil) {
        UnrecognizedMessageManager *manager = [UnrecognizedMessageManager shareManager];
        manager.currentInstance = self;
        manager.isClassMethod = NO;
        return manager;
    }else{
        return asdf;
    }
    
}

+ (id)jt_forwardingTargetForSelector:(SEL)aSelector{
    id asdf = [self jt_forwardingTargetForSelector:aSelector];
    
    if (MessageFarwordIsSystemClass(self)) {
        return asdf;
    }
    // 判斷類如果有自定義消息轉(zhuǎn)發(fā) 就不在處理
    if (asdf == nil) {
        UnrecognizedMessageManager *manager = [UnrecognizedMessageManager shareManager];
        manager.currentInstance = self;
        manager.isClassMethod = YES;
        return manager;
    }else{
        return asdf;
    }
}

3,未知消息管理類UnrecognizedMessageManager
新創(chuàng)建的類,經(jīng)過(guò)轉(zhuǎn)發(fā)后所有對(duì)象的未知消息都會(huì)轉(zhuǎn)發(fā)給它, 然后就可以在該對(duì)象的動(dòng)態(tài)方法解析這一步給未知消息添加一個(gè)統(tǒng)一的實(shí)現(xiàn), 然后在實(shí)現(xiàn)中打印錯(cuò)誤信息或者斷言。
UnrecognizedMessageManager.h文件如下

@interface UnrecognizedMessageManager : NSObject


+ (instancetype)shareManager;

@property (nonatomic,weak)id currentInstance;
@property (nonatomic,assign)BOOL isClassMethod;
@end

UnrecognizedMessageManager.m文件如下

// 要添加的處理未實(shí)現(xiàn)實(shí)例方法的方法
void instanceFunc(id self, SEL sel){
    
    UnrecognizedMessageManager *manager = [UnrecognizedMessageManager shareManager];
    NSString *className = NSStringFromClass([manager.currentInstance class]);
    NSString *funcName = NSStringFromSelector(sel);
    if (manager.isClassMethod) {
        NSLog(@"\n ?? +[%@ %@]:unrecognized selector sent to instance %p",className,funcName, manager.currentInstance);
    }else{
        NSLog(@"\n ?? -[%@ %@]:unrecognized selector sent to instance %p",className,funcName, manager.currentInstance);
    }
    
}


@implementation UnrecognizedMessageManager


+ (instancetype)shareManager{
    static UnrecognizedMessageManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[super allocWithZone:NULL] init];
    });
    return manager;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    return [UnrecognizedMessageManager shareManager];
}

- (id)copyWithZone:(NSZone *)zone{
    return [UnrecognizedMessageManager shareManager];
}

-(id)mutableCopyWithZone:(NSZone *)zone{
    return [UnrecognizedMessageManager shareManager];
}



+ (BOOL)resolveInstanceMethod:(SEL)sel{
    return class_addMethod([self class], sel, (IMP)instanceFunc, "V@:");;
}
@end

4,之后如果有未知消息的調(diào)用都會(huì)走消息轉(zhuǎn)發(fā)的流程,然后在hook的備用消息接受者方法中返回自定義的消息管理類, 然后消息管理類本身在動(dòng)態(tài)方法解析的時(shí)候統(tǒng)一添加了一個(gè)方法實(shí)現(xiàn),也就是說(shuō)只要有未知消息的調(diào)用就會(huì)走這個(gè)方法,然后在方法中可以進(jìn)行錯(cuò)誤信息的打印或者斷言,當(dāng)前的處理時(shí)打印信息,效果如下:

?? -[MessageTest testFunc:]:unrecognized selector sent to instance 0x7f9561703070
2019-12-14 09:41:03.600586+0800 RunTimeTest[2146:37381] 
 ?? +[ViewController classFuncTest]:unrecognized selector sent to instance 0x10b54d540
2019-12-14 09:41:04.380413+0800 RunTimeTest[2146:37381] 
 ?? +[MessageTest testClassMethod]:unrecognized selector sent to instance 0x10b54d608
2019-12-14 09:41:05.012581+0800 RunTimeTest[2146:37381] 
 ?? -[ViewController asdfadfasdf:]:unrecognized selector sent to instance 0x7f95617088c0
2019-12-14 09:41:09.057432+0800 RunTimeTest[2146:37381] 
 ?? -[ViewController did]:unrecognized selector sent to instance 0x7f95617088c0
?著作權(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)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒(méi)有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,626評(píng)論 1 32
  • 前段日子,我又看了一遍sunnyxx的一段有關(guān)runtime的分享會(huì)視頻(不要吐槽AV畫質(zhì)),結(jié)合這幾年在印象筆記...
    伯陽(yáng)閱讀 416評(píng)論 0 2
  • 對(duì)于 Runtime 的了解一直很少,面試的時(shí)候,總是會(huì)提及,因此開(kāi)始去了解 Runtime。在網(wǎng)上找尋這類資料和...
    Leafmure閱讀 277評(píng)論 0 0
  • 文中的實(shí)驗(yàn)代碼我放在了這個(gè)項(xiàng)目中。 以下內(nèi)容是我通過(guò)整理[這篇博客] (http://yulingtianxia....
    茗涙閱讀 1,026評(píng)論 0 6
  • 面向?qū)ο蟮娜筇匦裕悍庋b、繼承、多態(tài) OC內(nèi)存管理 _strong 引用計(jì)數(shù)器來(lái)控制對(duì)象的生命周期。 _weak...
    運(yùn)氣不夠技術(shù)湊閱讀 1,222評(píng)論 0 10

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