当我们调用一个实例或者类本身没有实现的方法的时候会发生一个经典的crash,unrecognized selector sent to instance。学习过runtime的知识后我们知道,利用runtimeAPI去hook系统的方法然后添加自己的操作,了解消息机制和消息转发的流程后就可以自己做一些消息转发。
标题所说的防护其实就是unrecognized selector sent to instance的防护,我利用消息转发的第二步备用消息接受者,转发给自己创建的一个专门处理未知消息的管理类,这样就可以达到即使调用了未知方法也不会发生crash的效果。
整个工具包含一个类和两个分类,没有侵入性,通过hook NSObject类的消息转发的方法统一把未知消息转发给自己创建的管理类,然后在管理类中处理未知消息(打印信息或者断言)。
类结构如下
1,NSObject+ExchangeMethodIMP 提供交换方法的分类,这样所有的对象都可以调用交换方法的API
NSObject+ExchangeMethodIMP.h
@interface NSObject (ExchangeMethodIMP)
+ (void)exchangeMethodWithClass: (Class)currentClass OriginalSel: (SEL)originalSel SwizzlingSel: (SEL)swizzlingSel IsClassMethod: (BOOL)isClassMethod;
@end
2,hook消息转发的分类 NSObject+MessageFarword
该分类的作用就是hook所有对象的消息转发的方法(具体包括-forwardingTargetForSelector:和+forwardingTargetForSelector:)然后统一把消息转发给自己创建的管理对象(单例UnrecognizedMessageManager),统一在管理对象中处理
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, ^{
// 交换方法 区分 实例方法 类方法
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系统的方法
- (id)jt_forwardingTargetForSelector:(SEL)aSelector{
id asdf = [self jt_forwardingTargetForSelector:aSelector];
if (MessageFarwordIsSystemClass(self)) {
return asdf;
}
// 判断类如果有自定义消息转发 就不在处理
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;
}
// 判断类如果有自定义消息转发 就不在处理
if (asdf == nil) {
UnrecognizedMessageManager *manager = [UnrecognizedMessageManager shareManager];
manager.currentInstance = self;
manager.isClassMethod = YES;
return manager;
}else{
return asdf;
}
}
3,未知消息管理类UnrecognizedMessageManager
新创建的类,经过转发后所有对象的未知消息都会转发给它, 然后就可以在该对象的动态方法解析这一步给未知消息添加一个统一的实现, 然后在实现中打印错误信息或者断言。
UnrecognizedMessageManager.h文件如下
@interface UnrecognizedMessageManager : NSObject
+ (instancetype)shareManager;
@property (nonatomic,weak)id currentInstance;
@property (nonatomic,assign)BOOL isClassMethod;
@end
UnrecognizedMessageManager.m文件如下
// 要添加的处理未实现实例方法的方法
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,之后如果有未知消息的调用都会走消息转发的流程,然后在hook的备用消息接受者方法中返回自定义的消息管理类, 然后消息管理类本身在动态方法解析的时候统一添加了一个方法实现,也就是说只要有未知消息的调用就会走这个方法,然后在方法中可以进行错误信息的打印或者断言,当前的处理时打印信息,效果如下:
🌹 -[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