Capture and process exception information in iOS published apps (GO)

Source: Internet
Author: User

In the development of iOS, we will encounter the case that the program throws an abnormal exit, and if it is in the process of debugging, the information of the exception is clear at a glance, but it is sometimes difficult to get the information of the exception in the published program.

iOS provides an exception to the processing API, we can add such a handler when the program starts, such a program when an exception can be done in this part of the information necessary processing, timely feedback to the developer.

The disadvantage is that not all of the program crashes are due to the occurrence of the exception can be caught, sometimes because of memory and other errors caused the program crashes, such information is not reflected here.

I made a simple class that was very basic and could add and get handler, catch an exception and write the information to Exception.txt in the app's Documens.

In fact, there are many ways to deal with it.
L For example can read this exception file to the server when the program is next up.
L or directly in the process code with OpenURL (mailto:) Call the way to send the message, the exception information directly into the message sent to the specified address.

The following is the complete code implementation.

Examples of usage scenarios:

#pragma mark-
#pragma mark Application Lifecycle

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Override point for customization after application launch.

[Window makekeyandvisible];
[Nduncaughtexceptionhandler Setdefaulthandler];
Nsarray *array = [Nsarray arraywithobject:@ "There is only one objective in this Arary,call index one, app'll crash and T Hrow an exception! "];
NSLog (@ "%@", [array objectatindex:1]);

return YES;
}

Basic interface Display:

#import <Foundation/Foundation.h>

@interface Nduncaughtexceptionhandler:nsobject {

}

+ (void) Setdefaulthandler;
+ (nsuncaughtexceptionhandler*) gethandler;

@end
You can also choose to set a custom handler to allow the user to select

Interface implementation Display
#import "NdUncaughtExceptionHandler.h"

NSString *applicationdocumentsdirectory () {
return [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];
}

void Uncaughtexceptionhandler (NSException *exception) {
Nsarray *arr = [exception callstacksymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];

NSString *url = [NSString stringwithformat:@ "============= exception crash report =============\nname:\n%@\nreason:\n%@\ ncallstacksymbols:\n%@ ",
Name,reason,[arr componentsjoinedbystring:@ "\ n"];
NSString *path = [Applicationdocumentsdirectory () stringbyappendingpathcomponent:@ "Exception.txt"];
[url Writetofile:path atomically:yes encoding:nsutf8stringencoding error:nil];
In addition to having the option to write to a file under the app, the information is sent to the server through subsequent processing
You can also choose to call the program that sent the message, send the message to the specified e-mail address
Or call a handler to process this information
}

@implementation Nduncaughtexceptionhandler

-(NSString *) applicationdocumentsdirectory {
return [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];
}

+ (void) Setdefaulthandler
{
Nssetuncaughtexceptionhandler (&uncaughtexceptionhandler);
}

+ (nsuncaughtexceptionhandler*) gethandler
{
return Nsgetuncaughtexceptionhandler ();
}

@end

Exception Crash report:
============= Exception crash report =============
Name
Nsrangeexception
Reason
-[nsarray Objectatindex:]: Index 1 beyond bounds [0.. 0]
Callstacksymbols:
0 corefoundation 0x02393919 __exceptionpreprocess + 185
1 LIBOBJC. A.dylib 0x024e15de Objc_exception_throw + 47
2 corefoundation 0x0238958c-[__nsarrayi objectatindex:] + 236
3 Uncaughte 0x000022e8-[uncaughteappdelegate application:didfinishlaunchingwithoptions:] + 15 7
4 UIKit 0x002b8543-[uiapplication _callinitializationdelegatesforurl:payload:suspended:] + 1163
5 UIKit 0x002ba9a1-[uiapplication _runwithurl:payload:launchorientation:statusbarstyle:st Atusbarhidden:] + 346
6 UIKit 0x002c4452-[uiapplication handleevent:withnewevent:] + 1958
7 UIKit 0x002bd074-[uiapplication sendevent:] + 71
8 UIKit 0x002c1ac4 _uiapplicationhandleevent + 7495
9 graphicsservices 0x02bf9afa Purpleeventcallback + 1578
Ten corefoundation 0x02374dc4 __cfrunloop_is_calling_out_to_a_source1_perform_function__ + 52
Corefoundation 0x022d5737 __cfrunloopdosource1 + 215
Corefoundation 0x022d29c3 __cfrunlooprun + 979
Corefoundation 0x022d2280 cfrunlooprunspecific + 208
Corefoundation 0X022D21A1 Cfrunloopruninmode + 97
UIKit 0x002ba226-[uiapplication _run] + 625
UIKit 0x002c5b58 Uiapplicationmain + 1160
Uncaughte 0x00002228 main + 102
Uncaughte 0x000021b9 start + 53

Capture and process exception information in iOS published apps (GO)

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.