很多开发小伙伴们经常私信问我一些问题:
1、若集成了腾讯Bugly或者友盟等等异常搜集的SDK,AvoidCrash会影响到它们的异常搜集吗?
2、为什么集成了AvoidCrash还是会报unrecognized selector sent to instance的异常?
疑惑1
1、若集成了腾讯Bugly或者友盟等等异常搜集的SDK,AvoidCrash会影响到它们的异常搜集吗?
首先要清楚的一点是,对于一些第三方crash信息搜集工具,比如Bugly或者友盟,它们只有当程序出现异常(崩溃)的时候才会搜集异常信息。而AvoidCrash的作用是,防止部分常见异常的发生,异常被AvoidCrash捕获了,程序就不会崩溃,第三方crash信息搜集工具就不会搜集到崩溃信息咯。
如何处理才能既防止崩溃,又搜集到崩溃信息
AvoidCrash若捕获到异常,将会发出一个通知:AvoidCrashNotification,监听该通知即可获取到原本将导致崩溃的具体信息。此时你可以利用Bugly的自定义异常接口将这些异常信息上传到Bugly。下面上代码,上图说明。
1、首先先来查看下Bugly提供的上报异常的接口
Bugly上报自定义异常接口
2、创建一个上报异常的工具类 BuglyManager(可以充分利用Bugly上报自定义异常功能,方便我们快速定位app出现的异常,下图展示了我所开发的项目中使用Bugly上报了哪些错误类型)
/** 上报错误信息 */
+ (void)reportErrorName:(NSString *)errorName errorReason:(NSString *)errorReason callStack:(NSArray *)aStackArray extraInfo:(NSDictionary *)info{
[Bugly reportExceptionWithCategory:3 errorName reason:errorReason callStack:aStackArray extraInfo:info terminateApp:NO];
}
3、在AppDelegate中初始化AvoidCrash并且监听通知:AvoidCrashNotification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[AvoidCrash makeAllEffective];
//监听通知:AvoidCrashNotification, 获取AvoidCrash捕获的崩溃日志的详细信息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:AvoidCrashNotification object:nil];
return YES;
}
- (void)dealwithCrashMessage:(NSNotification *)note {
//异常拦截并且通过bugly上报
NSDictionary *info = note.userInfo;
NSString *errorReason = [NSString stringWithFormat:@"【ErrorReason】%@========【ErrorPlace】%@========【DefaultToDo】%@========【ErrorName】%@", info[@"errorReason"], info[@"errorPlace"], info[@"defaultToDo"], info[@"errorName"]];
NSArray *callStack = info[@"callStackSymbols"];
[BuglyManager reportErrorName:Bugly_ErrorName_AvoidCrash errorReason:errorReason callStack:callStack extraInfo:nil];
4、写一个AvoidCrash可以拦截的异常
NSArray *array = @[@"iOS"];
NSString *string = array[100];
5、在Xcode控制台可以看到下图的输出
![](https://img.haomeiwen.com/i1594675/08d074f4516e425a.png)
6、去Bugly错误分析中查看
![](https://img.haomeiwen.com/i1594675/c5c3fd4474f20810.png)
![](https://img.haomeiwen.com/i1594675/5e939416566eec15.png)
疑惑2
2、为什么集成了AvoidCrash还是会报unrecognized selector sent to instance的异常?
很多小伙伴并没有下载过Demo查看AppDelegate中的代码和注释,其实在里面都写得比较详细了,并且在AvoidCrash.h文件中的部分方法也写了一些注意事项。
若要捕获 unrecognized selector sent to instance 类型的异常
1、首先查看下AvoidCrash中初始化AvoidCrash的两个方法
/**
*
* 开始生效.你可以在AppDelegate的didFinishLaunchingWithOptions方法中调用becomeEffective方法
* 【默认不开启 对”unrecognized selector sent to instance”防止崩溃的处理】
*
*/
+ (void)becomeEffective;
/**
* 相比于becomeEffective,增加
* 对”unrecognized selector sent to instance”防止崩溃的处理
*
* 但是必须配合setupClassStringsArr:使用
*/
+ (void)makeAllEffective;
2、若要捕获 unrecognized selector sent to instance 类型的异常,请使用[AvoidCrash makeAllEffective] 并且配合下面的两个方法使用。(这两个方法可以配合使用,可以同时使用)
/**
* 初始化一个需要防止”unrecognized selector sent to instance”的崩溃的类名数组
* ⚠️不可将@"NSObject"加入classStrings数组中
* ⚠️不可将UI前缀的字符串加入classStrings数组中
*/
+ (void)setupNoneSelClassStringsArr:(NSArray<NSString *> *)classStrings;
/**
* 初始化一个需要防止”unrecognized selector sent to instance”的崩溃的类名前缀的数组
* ⚠️不可将UI前缀的字符串(包括@"UI")加入classStringPrefixs数组中
* ⚠️不可将NS前缀的字符串(包括@"NS")加入classStringPrefixs数组中
*/
+ (void)setupNoneSelClassStringPrefixsArr:(NSArray<NSString *> *)classStringPrefixs;
3、具体的使用方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[AvoidCrash makeAllEffective];
//================================================
// 1、unrecognized selector sent to instance(方式1)
//================================================
//若出现unrecognized selector sent to instance并且控制台输出:
//-[__NSCFConstantString initWithName:age:height:weight:]: unrecognized selector sent to instance
//你可以将@"__NSCFConstantString"添加到如下数组中,当然,你也可以将它的父类添加到下面数组中
//比如,对于部分字符串,继承关系如下
//__NSCFConstantString --> __NSCFString --> NSMutableString --> NSString
//你可以将上面四个类随意一个添加到下面的数组中,建议直接填入 NSString
//我所开发的项目中所防止unrecognized selector sent to instance的类有下面几个,主要是防止后台数据格式错乱导致的崩溃。个人觉得若要防止后台接口数据错乱,用下面的几个类即可。
NSArray *noneSelClassStrings = @[
@"NSNull",
@"NSNumber",
@"NSString",
@"NSDictionary",
@"NSArray"
];
[AvoidCrash setupNoneSelClassStringsArr:noneSelClassStrings];
//================================================
// 2、unrecognized selector sent to instance(方式2)
//================================================
//若需要防止某个前缀的类的unrecognized selector sent to instance
//比如你所开发项目中使用的类的前缀:CC、DD
//你可以调用如下方法
NSArray *noneSelClassPrefix = @[
@"CC",
@"DD"
];
[AvoidCrash setupNoneSelClassStringPrefixsArr:noneSelClassPrefix];
//监听通知:AvoidCrashNotification, 获取AvoidCrash捕获的崩溃日志的详细信息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:AvoidCrashNotification object:nil];
return YES;
}
网友评论
解决办法就是[NSMutableDictionary dictionaryWithDictionary:[NSMutableDictionary dictionaryWithContentsOfFile:infoplistPath]];转一下。
应该是某种数据存储冲突了,导致在app在通过序列化缓存数据的时候,从后台来到前台后,就会闪退。
Crashed: com.apple.main-thread
EXC_BREAKPOINT 0x000000018279e378
Crashed: com.apple.main-thread
0 CoreFoundation 0x18279e378 __NSI0 + 704
1 CoreFoundation 0x18267d46c -[NSInvocation getArgument:atIndex:] + 160
2 Foundation 0x18312cca4 _NSGetValueWithMethod + 200
3 Foundation 0x1830d2964 -[NSObject(NSKeyValueCoding) valueForKey:] + 264
4 AccessibilityUtilities 0x1928522d0 __57-[NSObject(UIAccessibilitySafeCategory) safeValueForKey:]_block_invoke + 32
5 AccessibilityUtilities 0x192852754 -[NSObject(UIAccessibilitySafeCategory) _accessibilityPerformSafeValueKeyBlock:withKey:onClass:] + 92
6 AccessibilityUtilities 0x192851f70 -[NSObject(UIAccessibilitySafeCategory) safeValueForKey:] + 216
7 UIKit 0x1a2828908 -[UIViewControllerAccessibility _accessibilityLoadAccessibilityInformation] + 156
8 UIAccessibility 0x1944f40b4 -[UIAccessibilityInformationLoader _loadAccessibilityInformationOnMainThread:] + 960
9 Foundation 0x1831e33d4 __NSFireTimer + 88
10 CoreFoundation 0x18273faa8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 28
11 CoreFoundation 0x18273f76c __CFRunLoopDoTimer + 864
12 CoreFoundation 0x18273f010 __CFRunLoopDoTimers + 248
13 CoreFoundation 0x18273cb60 __CFRunLoopRun + 2168
14 CoreFoundation 0x18265cda8 CFRunLoopRunSpecific + 552
15 GraphicsServices 0x184642020 GSEventRunModal + 100
16 UIKit 0x18c67c758 UIApplicationMain + 236
17 myApp 0x1010ef2d0 main (main.m:13)
18 libdyld.dylib 0x1820edfc0 start + 4
AppDelegate.m
+ (void)load {
[AvoidCrash makeAllEffective];
[AvoidCrash setupNoneSelClassStringsArr:@[@"NSString",@"NSArray",@"NSNumber",@"NSDictionary"]];
}
这样吗?我试过也没有开机就闪退。
[UICachedDeviceWhiteColor shadowColor]: unrecognized selector sent to instance 0x103637c70'
*** First throw call stack:
- (id)__NSSingleObjectArrayIAvoidCrashObjectAtIndex:(NSUInteger)index {
id object = nil;
@try {
//崩溃到这里去了
object = [self __NSSingleObjectArrayIAvoidCrashObjectAtIndex:index];
}
@catch (NSException *exception) {
NSString *defaultToDo = AvoidCrashDefaultReturnNil;
[AvoidCrash noteErrorWithException:exception defaultToDo:defaultToDo];
}
@finally {
return object;
}
}
ms = [[self class] instanceMethodSignatureForSelector:aSelector];
if (ms == nil) {
ms = [AvoidCrashStubProxy instanceMethodSignatureForSelector:@selector(proxyMethod)];
}
//--------------------------------------------------
增加代码的健壮性。
NSArray *cuttingrray=[todayIncomeStr componentsSeparatedByString:@"."];
NSString *frontStr=cuttingrray.firstObject;
NSString *behindStr=cuttingrray.lastObject;
NSMutableAttributedString *attributeStr=[[NSMutableAttributedString alloc] initWithString:todayIncomeStr];
// 2.添加属性
[attributeStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12。
这个闪退也拦截不到啊
\\[.+\\] 实际上是 \[.+\], 这个匹配的是 [abdalj123(任意多个任意字符)] 这个是匹配必须以 [ 开头, 以 ] 结尾的, [ 和 ] 中间可以有任意多个字符.
所以就可以匹配出 - [lasjdljallsksjdl] 这样的方法来.
通过performSelector调用不存在的方法产生的崩溃无法防御。
* ⚠️不可将UI前缀的字符串加入classStrings数组中 *
⚠️不可将UI前缀的字符串(包括@"UI")加入classStringPrefixs数组中
* ⚠️不可将NS前缀的字符串(包括@"NS")加入classStringPrefixs数组中
是基于什么考虑的? 谢谢
@interface NSDictionary (AvoidCrash)<AvoidCrashProtocol>
@EnD
这个类里面的第30行
instance = [self avoidCrashDictionaryWithObjects:objects forKeys:keys count:cnt];