iOS exception (crash) output

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.