NSSetUncaughtExceptionHandler
Use your own program to capture crash and save it locally
-
Create a new class inherited from NSObject (the process of Xcode creating an empty project is omitted), named CatchCrash, and write in the h and m files:
void uncaughtExceptionHandler(NSException *exception) { // Exceptional stack information NSArray *stackArray = [exception callStackSymbols]; // Causes of abnormalities NSString *reason = [exception reason]; // Exception name NSString *name = [exception name]; NSString *exceptionInfo = [NSString stringWithFormat:@"Exception reason: %@\nException name: %@\nException stack: %@",name, reason, stackArray]; NSLog(@"%@", exceptionInfo); NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:stackArray]; [tmpArr insertObject:reason atIndex:0]; //Save it locally -- of course, you can upload the log the next time you start it up [exceptionInfo writeToFile:[NSString stringWithFormat:@"%@/Documents/error.log",NSHomeDirectory()] atomically:YES encoding:NSUTF8StringEncoding error:nil]; }
-
Add a class inherited from UIViewcontroller, named TestViewController.
-
Register CatchCrash exception handling method and write the following code in Appdelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. //Processing Method of Registered Message Processing Function NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); TestViewController *testVc = [[TestViewController alloc] init]; self.window.rootViewController = testVc; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
-
After the crash flickers back, upload the log file content to the server the next time you start app.
Enhancement characteristics
NSSet Uncaught Exception Handler is used for exception handling, but its function is very limited.
Most of the causes of crash, such as memory access errors, duplicate release errors, can not be helped, because this error throws out the Signal, so it must be specially Signal processing.
-
Define UncaghtExceptionHandler class, h file:
@interface UncaughtExceptionHandler : NSObject{ BOOL dismissed; } void InstallUncaughtExceptionHandler(); @end
m file:
```
void InstallUncaughtExceptionHandler()
{signal(SIGABRT, MySignalHandler); signal(SIGILL, MySignalHandler); signal(SIGSEGV, MySignalHandler); signal(SIGFPE, MySignalHandler); signal(SIGBUS, MySignalHandler); signal(SIGPIPE, MySignalHandler);
}
// When an app error generates the above signal, the custom function MySignalHandler is returned.
Specific realization strategy
```
-
Call the function in didFinish Launching WithOptions:
- (void)installUncaughtExceptionHandler { InstallUncaughtExceptionHandler(); }
Take two steps: All crashes are basically okay.
Get crash flicker log
-
XCode menu Window - > Organizer Select Devices - > Selected Mobile Phone - > Click the arrow on the left of the mobile phone name
Unknowwn and Crash are two types of logs that cause Crash and program exception Crash, respectively, when memory is not enough to reclaim memory kill ing applications.
-
Open the mobile phone - > Settings - > Privacy - > Diagnosis and Dosage - > Diagnosis and Dosage Data, which is the Crash log of all applications.
First find the iphone system path to store crash: var/mobile/Library/Logs/Crash Reporter. Find the path to store crash. Alas, it's hard to read (it's all nil read out by program).
-
Get the crash log of the user through iTunes Connect (Manage Your Applications - View Details - Crash Reports).