It is normal to encounter program crashes in iOS development, how to catch exception information when a program crashes and notify the developer, which is the method chosen by most software. 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
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
void Uncaughtexceptionhandler (NSException *exception) {
Nsarray *arr = [exception callstacksymbols];//get current call stack information
NSString *reason = [Exception reason];//is very important, is the cause of the crash
NSString *name = [Exception name];//exception type
NSLog (@ "Exception type:%@ \ Crash reason:%@ \ n Call stack info:%@", name, Reason, arr);
}
The above code is very simple, but the effect is very large.
Get to the day of the crash, how to send to the developer, there are generally the following two ways:
1. Persist the crash information locally and send the crash information as a log to the developer the next time the program starts.
2. Send it to the developer via email. However, this method needs to get the user's permission, because iOS can not send text messages or mail in the background, will pop up the email interface, only the user clicked Send to send. However, this approach is most in line with Apple's user-centric principle.
Sending a message code is also simple:
NSString *crashloginfo = [NSString stringwithformat:@ "exception type:%@ \ n crash reason:%@ \ n Call stack info:%@", n Ame, reason, arr];
NSString *urlstr = [NSString stringwithformat:@ mailto://[email protected]?subject=bug report &body= Thank you for your cooperation!
"
"Error Details:%@",
Crashloginfo];
Nsurl *url = [Nsurl urlwithstring:[urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
[[UIApplication sharedapplication] openurl:url];
The above is the common method of catching exceptions in iOS, you may wish to try
Capture program crash log in IOS