首先参考一下自己之前写的《method swizzing》这篇,特别是对类簇的methodSwizzing。
Container 类型的crash 指的是容器类的crash,常见的有NSArray/NSMutableArray/NSDictionary/NSMutableDictionary/NSCache的crash。 一些常见的越界、插入nil等错误操作均会导致此类crash发生。 由于产生的原因比较简单,就不展开来描述了。
该类crash虽然比较容易排查,但是其在app crash概率总比还是挺高,所以有必要对其进行防护。另外NSString也有类似的crash防护措施,在此篇中一并举例说明。
一、Container类型和NSString类型常见crash
(1) [aMutableDictionary setObject:nil forKey:]; object can not be nil.
(2) [aString hasSuffix:nil]; nil argument crash.
[aString hasPrefix:nil]; nil argument crash.
(3) aString = [NSMutableString stringWithString:nil];nil argument crash.
(4) aString = [[NSString alloc] initWithString:nil]; nil argument crash.
(5) aURL = [NSURL fileURLWithPath:nil]; nil argument crash.
(6) NSArray 数组越界 crash。
二、使用method swizzing对常见crash防护举例
这里对NSString、NSMutableDictionary和NSArray常见的几个crash举例如下:
(1)对NSString的hasSuffix:和hasPrefix:进行预防:
#import "NSString+CrashGurad.h"
#import <objc/runtime.h>
@implementation NSString (CrashGurad)
#pragma mark Class Method
+(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[[self class] swizzedMethod:@selector(hasSuffix:) withMethod:@selector(crashGuard_hasSuffix:)];
[[self class] swizzedMethod:@selector(hasPrefix:) withMethod:@selector(crashGuard_hasPrefix:)];
});
}
+(void)swizzedMethod:(SEL)originalSelector withMethod:(SEL )swizzledSelector {
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSCFConstantString"), originalSelector);
Method toMethod = class_getInstanceMethod(objc_getClass("__NSCFConstantString"), swizzledSelector);
method_exchangeImplementations(fromMethod, toMethod);
}
#pragma mark Swizzled Method
-(BOOL)crashGuard_hasSuffix:(NSString *)str {
if(!str){
// 打印崩溃信息,栈信息 等
NSLog(@"selector \"hasSuffix\" crash for the the suffix is nil!");
return NO;
} else {
return [self crashGuard_hasSuffix:str];
}
}
- (BOOL)crashGuard_hasPrefix:(NSString *)str {
if(!str){
// 打印崩溃信息,栈信息 等
NSLog(@"selector \"hasPrefix\" crash for the the prefix is nil!");
return NO;
} else {
return [self crashGuard_hasPrefix:str];
}
}
(2)对NSArray的objectAtIndex:的crash预防如下:
#import "NSArray+CrashGuard.h"
#import <objc/runtime.h>
@implementation NSArray (CrashGuard)
+(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(crashGuard_objectAtIndex:));
method_exchangeImplementations(fromMethod, toMethod);
});
}
#pragma mark Swizzled Method
-(id)crashGuard_objectAtIndex:(NSUInteger)index {
if(self.count-1 < index) {
// 打印崩溃信息,栈信息 等
NSLog(@"selector \"objectAtIndex\" crash for the index beyond the boundary!");
return nil;
} else {
return [self crashGuard_objectAtIndex:index];
}
}
@end
3)对NSMutableDictionary的setObject:forKey:的crash预防如下:
#import "NSMutableDictionary+CrashGuard.h"
#import <objc/runtime.h>
@implementation NSMutableDictionary (CrashGuard)
+(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSDictionaryM"), @selector(setObject:forKey:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSDictionaryM"), @selector(crashGuard_setObject:forKey:));
method_exchangeImplementations(fromMethod, toMethod);
});
}
#pragma mark Swizzled Method
-(void)crashGuard_setObject:(id)object forKey:(NSString *)key {
if(!object) {
// 打印崩溃信息,栈信息 等
NSLog(@"selector \"setObject:forKey:\" crash for the the object is nil!");
} else {
[self crashGuard_setObject:object forKey:key];
}
}
@end
总结:
Container crash 类型和NSString类型的crash防护方案比较简单,针对于NSArray/NSMutableArray/NSDictionary/NSMutableDictionary的一些常用的会导致崩溃的API进行method swizzling,然后在swizzle的新方法中加入一些条件限制和判断,从而让这些API变的安全。当然,在程序进入这些swizzle的方法后,我们需要及时记录并上传相关的日志信息到后台服务器,并及时review相关的内容,将迭代中将有隐患的代码改掉。
网友评论