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 Handlerwhen the program starts, such a program when an exception can be done in this part of the information to do the necessary processing, Timely feedback to the developer.
First define a class Signalhandler
@interface signalhandler: nsobject
+ (instancetype) Instance;
-(void) Setexceptionhandler;
@end
Implement SIGNALHANDLER.M
#import "SignalHandler.h"
#import <UIKit/UIKit.h>
#include <libkern/OSAtomic.h>
#include <execinfo.h>
@interface signalhandler ()
{
BOOL isdismissed;
}
-(void) HandleException1: (nsexception *) exception;
@end
callback function after capturing the signal
void handleexception (nsexception *exception)
{
// handling exception messages
[[signalhandler Instance] performselectoronmainthread:@selector( HandleException1:) withobject: Exception waituntildone:YES];
}
@implementation Signalhandler
Static signalhandler *s_signalhandler = nil;
+ (instancetype) instance{
static dispatch_once_t oncetoken;
dispatch_once(&oncetoken, ^{
if (s_signalhandler = = Nil) {
S_signalhandler = [[signalhandler alloc] init];
}
});
return s_signalhandler;
}
-(void) Setexceptionhandler
{
Nssetuncaughtexceptionhandler(&handleexception);
}
methods used to handle exceptions
-(void) HandleException1: (nsexception *) exception
{
cfrunloopref runloop = cfrunloopgetcurrent();
cfarrayref allmodes = cfrunloopcopyallmodes(runloop);
// exception stack information
nsarray *stackarray = [exception callstacksymbols];
// Causes of anomalies
nsstring *reason = [exception reason];
// exception name
nsstring *name = [exception name];
nsstring *exceptioninfo = [nsstring stringwithformat:@ " cause:%@\n Name:%@\ N Stacks:%@ ", name, Reason, Stackarray];
NSLog(@ "%@", exceptioninfo);
// Here you can also save file
uialertview *alertview = [[uialertview alloc] initwithtitle:@ "There's a problem with the program. " message: Exceptioninfo delegate:self cancelbuttontitle:@" Cancel " otherbuttontitles:nil";
[Alertview show];
// when receiving an exception handling message, let the program start runloopto prevent the program from dying
while (! Isdismissed) {
for (nsstring *mode in (__bridge nsarray *) allmodes)
{
Cfrunloopruninmode((cfstringref) mode, 0.001, false);
}
}
// when clicking the Cancel button of the popup view Oh , isdimissed = Yes, the top loop jumps out
cfrelease(allmodes);
Nssetuncaughtexceptionhandler(NULL);
}
-(void) Alertview: (uialertview *) Analertview clickedbuttonatindex: (nsinteger) anindex
{
// because this pop-up view has only one Cancel button, the isdimsmissed variable is modified directly.
isdismissed = YES;
}
Finally, you need to
Add in Didfinishlaunchingwithoptions
[[signalhandler Instance] setexceptionhandler];
Once you're done, you can verify it and add the following code
nsarray *array = [nsarray arraywithobjects:@ "1", Nil];
[Array objectatindex:2];
IOS Capture Program exception