1. Add a button action
-(Ibaction) Throwfakeexception: (UIButton *) Sender { * e = [[NSException alloc] Initwithname:@ " fakeexception " Reason:@" Thedeveloper Sucks! " userinfo:[nsdictionary dictionarywithobject:@"Extra info" forkey:@" Key"]]; [E raise];}
2. Register a handler function with the Nssetuncaughtexception function, which is a void function that accepts a nsexception reference as a unique parameter.
////APPDELEGATE.M//errorhanding////Created by Feng Min on 16/4/29.//copyright©2016 year fengmin. All rights reserved.//#import "AppDelegate.h"#import<MessageUI/MessageUI.h>#import<MessageUI/MFMailComposeViewController.h>@interfaceAppdelegate () <uialertviewdelegate, mfmailcomposeviewcontrollerdelegate>@end@implementationappdelegatevoidExceptionhandler (NSException *exception) {NSLog (@"uncaught exception:%@\nreason:%@\nuser info:%@\ncall stack:%@", Exception.name,exception.reason,exception.userinfo,exception.callstacksymbols); //Set FlagNsuserdefaults * settings =[Nsuserdefaults Standarduserdefaults]; [Settings Setbool:yes Forkey:@"Exceptionoccurredonlastrun"]; [Settings synchronize];}-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions {
Predefined conditional macros that use our exception handling code only when the application is running on a real device. #if! Target_iphone_simulator//Default excepion handling codeNsuserdefaults * settings =[Nsuserdefaults Standarduserdefaults]; if([Settings Boolforkey:@"Exceptionoccurredonlastrun"]) { //Reset exception occurred flag[Settings Setbool:no Forkey:@"Exceptionoccurredonlastrunkey"]; [Settings synchronize]; //Notify the userUialertview * Altert = [[Uialertview alloc] Initwithtitle:@"We are sorry!"Message@"An error occurred on the previous run!" Delegate: Nil Cancelbuttontitle:@"Dismiss"Otherbuttontitles:nil]; [Altert addbuttonwithtitle:@"Email a report"]; [Altert show]; }Else{ //Register exception handle functionNssetuncaughtexceptionhandler (&Exceptionhandler); //Redirect stderr output stream to fileNsarray * paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString* Documentspath = [Paths Objectatindex:0]; NSString* Stderrpath = [Documentspath stringbyappendingpathcomponent:@"Stderr.log"]; Freopen ([Stderrpath cstringusingencoding:nsasciistringencoding],"W", stderr); }#endif returnYES;}- (void) Alertview: (Uialertview *) Alertview Diddismisswithbuttonindex: (nsinteger) buttonindex{Nsarray* Paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString* Documentspath = [Paths Objectatindex:0]; NSString* Stderrpath = [Documentspath stringbyappendingpathcomponent:@"Stderr.log"]; if(Buttonindex = =1) { //Email a reportMfmailcomposeviewcontroller * Mailcomposer =[[Mfmailcomposeviewcontroller alloc] init]; Mailcomposer.mailcomposedelegate=Self ; [Mailcomposer setsubject:@"Error Report"]; [Mailcomposer settorecipients:[nsarray Arraywithobject:@"[email protected]"]]; //Attach log FileNsarray * paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString* Documentspath = [Paths Objectatindex:0]; NSString* Stderrpaht = [Documentspath stringbyappendingpathcomponent:@"Stderr.log"]; NSData* data =[NSData Datawithcontentsoffile:stderrpath]; [Mailcomposer addattachmentdata:data MimeType:@"Text/xml"FileName:@"Stderr.log"]; Uidevice* device =[Uidevice Currentdevice]; NSString* Emailbody = [NSString stringWithFormat:@"My model:%@\nmy os:%@\nmy version:%@", device.model,device.systemname,device.systemversion]; [Mailcomposer setmessagebody:emailbody Ishtml:no]; [Self.window.rootViewController presentviewcontroller:mailcomposer Animated:yes Completion:^{ }]; } nssetuncaughtexceptionhandler (&Exceptionhandler); //Redirect stderr output stream to fileFreopen ([Stderrpath cstringusingencoding:nsasciistringencoding],"W", stderr);}- (void) Mailcomposecontroller: (Mfmailcomposeviewcontroller *) controller didfinishwithresult: (mfmailcomposeresult) result Error: (Nserror *) error{[self.window.rootViewController dismissviewcontrolleranimated:yes Completion:^{ }];}
Strategy 1-11: Handling Exceptions