Recently, I suddenly think of a friend League SDK comes with a feature: will be the exception of the flash back to the server, (Stackflow,github) found some information, wrote a demo of their own, think for a long time did not write a blog, by the way to share.
In fact, not only ios,android logic is also the same, crash log is actually the system comes with, when the flash back, will be crash printing, the use of the IDE's classmates can be very obvious debugging to see the wrong information, positioning problems.
After the program is packaged to the user, we want to see how the program works, by writing these crash logs to the file (too late to upload, creating a link), and passing the server on the next launch.
The simple code logic is to create a try catch message response that is processed.
Here is a sample code for the demo:
//Here is the main exception handling example
voidUncaughtexceptionhandler (NSException *exception) { //stack information for exceptionsNsarray * Stackarray =[Exception callstacksymbols]; //Why the exception occurredNSString * reason =[Exception reason]; //Exception nameNSString * 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 to local-of course you can upload this log at the next launch[Exceptioninfo writetofile:[nsstring stringWithFormat:@"%@/documents/error.log", Nshomedirectory ()] Atomically:yes encoding:nsutf8stringencoding Error:nil]; } - (void) Printerror {nserror*error; NSString*textfilecontents = [NSString stringwithcontentsoffile:[nsstring stringWithFormat:@"%@/documents/error.log", Nshomedirectory ()] encoding:nsutf8stringencoding error: &ERROR]; if(Textfilecontents = =Nil) {NSLog (@"Error reading text file.%@", [Error Localizedfailurereason]); } Nsarray*lines = [Textfilecontents componentsseparatedbystring:@"\ n"]; NSLog (@"%@", textfilecontents); NSLog (@"Number of lines in the file:%d", [lines Count]);}
Called directly when the program starts
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions { // Catching exceptions Nssetuncaughtexceptionhandler (&Uncaughtexceptionhandler); = [[UIWindow alloc]initwithframe:[uiscreen mainscreen].bounds]; [Self.window makekeyandvisible]; = [[Viewcontroller alloc]init]; [[Catchcrash alloc] printerror]; return YES;}
The basic logic is this, and the code is very simple.
iOS exception (crash) output