UncaughtExceptionHandler
生活随笔
收集整理的這篇文章主要介紹了
UncaughtExceptionHandler
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
雖然大家都不愿意看到程序崩潰,但可能崩潰是每個應用必須面對的現實,既然崩潰已經發生,無法阻擋了,那我們就讓它崩也崩得淡定點吧。
IOS SDK中提供了一個現成的函數 NSSetUncaughtExceptionHandler 用來做異常處理,但功能非常有限,而引起崩潰的大多數原因如:內存訪問錯誤,重復釋放等錯誤就無能為力了,因為這種錯誤它拋出的是Signal,所以必須要專門做Signal處理。首先定義一個UncaughtExceptionHandler類,代碼如下:
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface UncaughtExceptionHandler : NSObject { BOOL dismissed; } +(void) InstallUncaughtExceptionHandler; @end//利用 NSSetUncaughtExceptionHandler,當程序異常退出的時候,可以先進行處理,然后做一些自定義的動作,比如下面一段代碼,就是網上有人寫的,直接在發生異常時給某人發送郵件,</span> void UncaughtExceptionHandlers (NSException *exception); #import "UncaughtExceptionHandler.h" #include <libkern/OSAtomic.h> #include <execinfo.h> NSString * const UncaughtExceptionHandlerSignalExceptionName = @"UncaughtExceptionHandlerSignalExceptionName"; NSString * const UncaughtExceptionHandlerSignalKey = @"UncaughtExceptionHandlerSignalKey"; NSString * const UncaughtExceptionHandlerAddressesKey = @"UncaughtExceptionHandlerAddressesKey"; volatile int32_t UncaughtExceptionCount = 0; const int32_t UncaughtExceptionMaximum = 10; const NSInteger UncaughtExceptionHandlerSkipAddressCount = 4; const NSInteger UncaughtExceptionHandlerReportAddressCount = 5; NSString* getAppInfo() { NSString *appInfo = [NSString stringWithFormat:@"App : %@ %@(%@)\nDevice : %@\nOS Version : %@ %@\n", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"], [UIDevice currentDevice].model, [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion]; // [UIDevice currentDevice].uniqueIdentifier]; NSLog(@"Crash!!!! %@", appInfo); return appInfo; } void MySignalHandler(int signal) { int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount); if (exceptionCount > UncaughtExceptionMaximum) { return; } if(signal==11) {//比較坑爹的是 我遇到的一個問題只有iPhone5出現問題 但是我這邊測試的沒有iPhone5 無法直接log 可能是內存不足 果然 刪除幾個應用就可以了 所以加了這句 UIAlertView * tip2 = [[UIAlertView alloc]initWithTitle:@"可能原因:key" message:@"內存不足" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; [tip2 show]; [tip2 release]; } NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithInt:signal] forKey:UncaughtExceptionHandlerSignalKey]; NSArray *callStack = [UncaughtExceptionHandler backtrace]; [userInfo setObject:callStack forKey:UncaughtExceptionHandlerAddressesKey]; [[[[UncaughtExceptionHandler alloc] init] autorelease] performSelectorOnMainThread:@selector(handleException:) withObject: [NSException exceptionWithName:UncaughtExceptionHandlerSignalExceptionName reason: [NSString stringWithFormat: NSLocalizedString(@"Signal %d was raised.\n" @"%@", nil), signal, getAppInfo()] userInfo: [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:signal] forKey:UncaughtExceptionHandlerSignalKey]] waitUntilDone:YES]; } @implementation UncaughtExceptionHandler +(void) InstallUncaughtExceptionHandler { signal(SIGABRT, MySignalHandler); signal(SIGILL, MySignalHandler); signal(SIGSEGV, MySignalHandler); signal(SIGFPE, MySignalHandler); signal(SIGBUS, MySignalHandler); signal(SIGPIPE, MySignalHandler); } + (NSArray *)backtrace { void* callstack[128]; int frames = backtrace(callstack, 128); char **strs = backtrace_symbols(callstack, frames); int i; NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames]; for ( i = UncaughtExceptionHandlerSkipAddressCount; i < UncaughtExceptionHandlerSkipAddressCount + UncaughtExceptionHandlerReportAddressCount; i++) { [backtrace addObject:[NSString stringWithUTF8String:strs[i]]]; } free(strs); return backtrace; } - (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex { if (anIndex == 0) { dismissed = YES; } } - (void)handleException:(NSException *)exception { UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Unhandled exception", nil) message:[NSString stringWithFormat:NSLocalizedString( @"You can try to continue but the application may be unstable.\n" @"%@\n%@", nil), [exception reason], [[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]] delegate:self cancelButtonTitle:NSLocalizedString(@"Quit", nil) otherButtonTitles:NSLocalizedString(@"Continue", nil), nil] autorelease]; [alert show]; CFRunLoopRef runLoop = CFRunLoopGetCurrent(); CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop); while (!dismissed) { for (NSString *mode in (NSArray *)allModes) { CFRunLoopRunInMode((CFStringRef)mode, 0.001, false); } } CFRelease(allModes); NSSetUncaughtExceptionHandler(NULL); signal(SIGABRT, SIG_DFL); signal(SIGILL, SIG_DFL); signal(SIGSEGV, SIG_DFL); signal(SIGFPE, SIG_DFL); signal(SIGBUS, SIG_DFL); signal(SIGPIPE, SIG_DFL); if ([[exception name] isEqual:UncaughtExceptionHandlerSignalExceptionName]) { kill(getpid(), [[[exception userInfo] objectForKey:UncaughtExceptionHandlerSignalKey] intValue]); } else { [exception raise]; } } void UncaughtExceptionHandlers (NSException *exception) { NSArray *arr = [exception callStackSymbols]; NSString *reason = [exception reason]; NSString *name = [exception name]; NSString *urlStr = [NSString stringWithFormat:@"mailto://1140454645@qq.com?subject=bug報告&body=感謝您的配合!<br><br><br>" "錯誤詳情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@", name,reason,[arr componentsJoinedByString:@"<br>"]]; NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url]; //或者直接用代碼,輸入這個崩潰信息,以便在console中進一步分析錯誤原因 NSLog(@"1heqin, CRASH: %@", exception); NSLog(@"heqin, Stack Trace: %@", [exception callStackSymbols]); } @end //然后在delegate文件里面- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函數里面 [UncaughtExceptionHandler InstallUncaughtExceptionHandler]; NSSetUncaughtExceptionHandler (&UncaughtExceptionHandlers);
?
總結
以上是生活随笔為你收集整理的UncaughtExceptionHandler的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线程_内部锁和同步
- 下一篇: BZOJ3498 : PA2009 Ca