Capture and handle exceptions in IOS published apps

Source: Internet
Author: User

Capture and handle exceptions in IOS published apps

 

 

During iOS development, the program will throw an exception and exit. If the exception information is clear during debugging, but if it is in a released program, it is sometimes difficult to obtain abnormal information.

 

IOS provides an API for handling exceptions. You can add handler when the program starts. When such a program encounters an exception, you can perform necessary processing on the information, timely feedback to developers.

 

The disadvantage is that not all program crashes because of exceptions that can be captured. Sometimes, the program crashes due to memory and other errors, this information is not here.

 

Running.

 

There are still many solutions.

L for example, the exception file can be read to the server at the next time when the program starts up.

L or you can simply use the Openurl method (mailto :) In the processing code to call the mail sending method, and change the exception information directly to the mail sending Address.

 

The following is the complete code implementation.

 

Use Cases:

 

# Pragma mark-

# Pragma mark application Lifecycle

 

-(Bool) Application :( uiapplication *) Application
Didfinishlaunchingwitexceptions :( 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 will crash and throw 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 set custom handler for the user to select

 

Interface implementation

# 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: @ "=============== abnormal 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: nsutf8stringencodingerror: Nil];

// In addition to writing to a file under the application, the information can be sent to the server through subsequent processing.

// You can also call the email sending program to send information to the specified email 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

 

Abnormal crash report:

================ Abnormal crash report ====================

Name:

Nsangeexception

Reason:

* **-[Nsarray objectatindex:]: Index 1 beyond bounds [0 .. 0]

Callstacksymbols:

0 corefoundation 0x02393919 _ predictionpreprocess + 185

1 libobjc. A. dylib 0x024e15de objc_exception_throw + 47

2 corefoundation 0x0238958c-[_ nsarrayi objectatindex:] + 236

3 uncaughte 0x000022e8-[uncaughteappdelegate application: didfinishlaunchingwitexceptions:] + 157

4 uikit 0x002b8543-[uiapplication _ callinitializationdelegatesforurl: payload: suincluded:] + 1163

5 uikit 0x002ba9a1-[uiapplication _ runwithurl: payload: launchorientation: statusbarstyle: statusbarhidden:] + 346

6 uikit 0x002c4452-[uiapplication handleevent: withnewevent:] + 1958

7 uikit 0x002bd074-[uiapplication sendevent:] + 71

8 uikit 0x002c1ac4 _ uiapplicationhandleevent + 7495

9 graphicsservices 0x02bf9afa purpleeventcallback + 1578

10 corefoundation 0x02374dc4 _ cfrunloop_is_calling_out_to_a_source1_m_function _ + 52

11 corefoundation 0x022d5737 _ cfrunloopdosource1 + 215

12 corefoundation 0x022d29c3 _ cfrunlooprun + 979

13 corefoundation 0x022d2280 cfrunlooprunspecific + 208

14 corefoundation 0x022d21a1 cfrunloopruninmode + 97

15. uikit 0x002ba226-[uiapplication _ run] + 625

16 uikit 0x002c5b58 uiapplicationmain + 1160

17 uncaughte 0x00002228 main ++ 102

18 uncaughte 0x000021b9 start + 53

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.