In recent months, at the company's arrangement and my personal requirements, I have been involved in iPhone development, mainly in the application field. I participated in the development of two iPhone projects, it has been compared to the AppStore, so you can finally take a rest for a while.
A problem often occurs during project development. Every time a hard version is released to the tester for testing, we get bugs. In the face of these problems, some problems are still easy to find, and those problems that are hard to reproduce are left speechless. If it is not on a simulator or our device is not connected to a PC, how can we debug our program? If the app cannot be sent to the AppStore, the user may encounter a problem during use. We certainly hope to be able to perceive the problem and modify and improve our app when updating the next version.
This leads to a requirement: capture exceptions during application use and submit them to the server or developer's mailbox.
Let's talk about the code.
1. It is used to handle exceptions not captured by try... catch...
Void UncaughtExceptionHandler (NSException * exception ){
// Abnormal stack information
NSArray * stackArray = [exception callStackSymbols];
// Cause of exception
NSString * reason = [exception reason];
// Exception name
NSString * name = [exception name];
NSString * syserror = [NSString stringWithFormat: @ "Exception name: % @ \ n cause of exception: % @ \ n exception stack information: % @", name, reason, stackArray];
NSLog (@ "% @", syserror );
// Send an exception to the developer's email address or send it to the server
}
2. Replace the original Handler that handles exceptions in the application startup.
// Save the Handler for handling system exceptions
_ UncaughtExceptionHandler = NSGetUncaughtExceptionHandler ();
// Set the Handler to handle exceptions
NSSetUncaughtExceptionHandler (& UncaughtExceptionHandler );
3. Restore the Handler for exception handling when the application exits.
-(Void) applicationWillTerminate :( UIApplication *) application
{
// Restore to the Handler for system Exception Handling
NSSetUncaughtExceptionHandler (_ uncaughtExceptionHandler );
}
In this way, if the tester or user finds the collapse problem, we can easily find the cause and correct it.
If you think there are any shortcomings in this strategy, leave a message to discuss it.
From ahutzh