It is normal to encounter a program crash in iOS development, how to catch the exception information when the program crashes and notify the developer?
Here's how to do this in iOS:
1. Add an exception capture listener during program startup to handle callback actions when the program crashes
| 1 |
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler); |
Official Document Description: Sets the top-level error-handling function where you can perform last-minute logging before the program terminates.
Uncaughtexceptionhandler is a function pointer that needs to be implemented and can take the name we want. When a program crashes abnormally, the function is called, which is the same concept as the callback function in c,c++.
2. Implement your own processing functions
| 1234567 |
voidUncaughtExceptionHandler(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);} |
The above code is very simple, but the effect is very large.
iOS Dev-Capture program crash log