當(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