在开发中经常会遇到崩溃的情况,在调试过程中可以直接看到崩溃信息,但是发布之后查看崩溃信息就比较困难了。iOS提供了NSSetUncaughtExceptionHandler用来做异常处理,代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
return YES;
}
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols]; //得到当前调用栈信息
NSString *reason = [exception reason]; //非常重要,就是崩溃的原因
NSString *name = [exception name]; //异常类型
NSLog(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);
//崩溃后的处理,如将崩溃信息发送到服务器或发送邮件等
}