1.通过类别给类添加方法用来替换掉原来存在潜在奔溃的方法
2.利用runtime方法交换技术,将系统方法替换成我们给类添加的新方法
3.try catch捕获异常
如果想查看例子
可以用CocoaPods pod search AvoidCrash 框架
用法很简单:
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//启动防止崩溃功能
[AvoidCrash becomeEffective];
//监听通知:AvoidCrashNotification, 获取AvoidCrash捕获的崩溃日志的详细信息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:AvoidCrashNotification object:nil];
return YES;
}
#pragma mark - 报错处理
- (void)dealwithCrashMessage:(NSNotification *)note {
//注意:所有的信息都在userInfo中
//你可以在这里收集相应的崩溃信息进行相应的处理(比如传到自己服务器)
NSLog(@"\n\n在AppDelegate中 方法:dealwithCrashMessage打印\n\n\n\n\n%@\n\n\n\n",note.userInfo);
}