Capture program crash log in IOS

Source: Internet
Author: User

Capture program crash log in IOS(2014-04-22 17:35:59) reproduced
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 a common way to catch exceptions in iOS, you can try!

Capture program crash log in IOS

Related Article

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.