1. 程式人生 > >【IOS】異常捕獲 拒絕閃退 讓應用從容的崩潰 UncaughtExceptionHandler

【IOS】異常捕獲 拒絕閃退 讓應用從容的崩潰 UncaughtExceptionHandler

mode tis ring enc 原因 include equal ipad ani


盡管大家都不願意看到程序崩潰,但可能崩潰是每一個應用必須面對的現實。既然崩潰已經發生。無法阻擋了。那我們就讓它崩也崩得淡定點吧。

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://[email protected]?

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);



【IOS】異常捕獲 拒絕閃退 讓應用從容的崩潰 UncaughtExceptionHandler