IOS應用之一--異常處理(UncaughtExceptionHandler)

來源:互聯網
上載者:User

標籤:

使用原因

      iOS開發中我們會遇到程式拋出異常退出的情況,如果是在調試的過程中,異常的資訊是一目瞭然,但是如果是在已經發布的程式中,擷取異常的資訊有時候是比較困難的。

好處與缺點  

     iOS提供了異常發生的處理API,我們在程式啟動的時候可以添加這樣的Handler,這樣的程式發生異常的時候就可以對這一部分的資訊進行必要的處理,適時的反饋給開發人員。  

     不足的地方是,並不是所有的程式崩潰都是由於發生可以捕捉的異常的,有些時候是因為記憶體等一些其他的錯誤導致程式的崩潰,這樣的資訊是不在這裡體現的。  

常用的處理方式

     第一種方式:作基本的操作,可以添加和擷取Handler,捕獲到異常後將資訊寫入到app的Documens下的Exception.txt中。  

     第二種方式:比如可以在程式下一次起來的時候讀取這個異常檔案發生到服務端

     第三種方式:或者直接就是在處理代碼中用openurl的方式(mailto:)調用發送郵件的方式,將異常資訊直接變成郵件發送到指定地址,其實還有很多的處理的辦法。  

執行個體 :

#pragma mark  代理類中的寫法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[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;
}

異常基本介面展示:

#import <Foundation/Foundation.h>

@interface NdUncaughtExceptionHandler : NSObject {

}

+ (void)setDefaultHandler;
+ (NSUncaughtExceptionHandler*)getHandler;

@end
//還可以選擇設定自訂的handler,讓使用者取選擇

介面實現展示
#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:@"=============異常崩潰報告=============\nname:\n%@\nreason:\n%@\ncallStackSymbols:\n%@",
name,reason,[arr componentsJoinedByString:@"\n"]];
NSString *path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];
[url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
//除了可以選擇寫到應用下的某個檔案,通過後續處理將資訊發送到伺服器等
//還可以選擇調用發送郵件的的程式,發送資訊到指定的郵件地址
//或者調用某個處理常式來處理這個資訊
}

@implementation NdUncaughtExceptionHandler

-(NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

+ (void)setDefaultHandler
{
NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
}

+ (NSUncaughtExceptionHandler*)getHandler
{
return NSGetUncaughtExceptionHandler();
}

@end

異常崩潰報告:
=============異常崩潰報告=============
name:
NSRangeException
reason:
*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]
callStackSymbols:
0 CoreFoundation 0x02393919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x024e15de objc_exception_throw + 47
2 CoreFoundation 0x0238958c -[__NSArrayI objectAtIndex:] + 236
3 UncaughtE 0x000022e8 -[UncaughtEAppDelegate application:didFinishLaunchingWithOptions:] + 157
4 UIKit 0x002b8543 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 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_PERFORM_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

不足的地方是,並不是所有的程式崩潰都是由於發生可以捕捉的異常的,有些時候引起崩潰的大多數原因如:記憶體訪問錯誤,重複釋放等錯誤就無能為力了,因為這種錯誤它拋出的是Signal,所以必須要專門做Signal處理。首先定義一個UncaughtExceptionHandler類,.h標頭檔的代碼如下:


UncaughtExceptionHandler類,.h標頭檔的代碼如下:

1
2
3
4
5
6
#import

@interface UncaughtExceptionHandler : NSObject{
BOOL dismissed;
}
@end
1
void InstallUncaughtExceptionHandler();
然後在.mm檔案實現InstallUncaughtExceptionHandler(),如下:
void InstallUncaughtExceptionHandler(){
signal(SIGABRT, MySignalHandler);
signal(SIGILL, MySignalHandler);
signal(SIGSEGV, MySignalHandler);
signal(SIGFPE, MySignalHandler);
signal(SIGBUS, MySignalHandler);
signal(SIGPIPE, MySignalHandler);
}
這樣,當應用發生錯誤而產生上述Signal後,就將會進入我們自訂的回呼函數MySignalHandler。為了得到崩潰時的現場資訊,還可以加入一些擷取CallTrace及裝置資訊的代碼,.mm檔案的完整代碼如下:

#import "UncaughtExceptionHandler.h"
#include #include

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;

@implementation UncaughtExceptionHandler

+ (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])

{
<span style="white-space:pre"></span>kill(getpid(), [[[exception userInfo] objectForKey:UncaughtExceptionHandlerSignalKey] intValue]);
}
else
{
[exception raise];
}
}
@end

NSString* getAppInfo() { NSString *appInfo = [NSString stringWithFormat:@"App : %@ %@(%@)\nDevice : %@\nOS Version : %@ %@\nUDID : %@\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;
}

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

}

void InstallUncaughtExceptionHandler()

{

signal(SIGABRT, MySignalHandler);

signal(SIGILL, MySignalHandler);

signal(SIGSEGV, MySignalHandler);

signal(SIGFPE, MySignalHandler);

signal(SIGBUS, MySignalHandler);

signal(SIGPIPE, MySignalHandler);

}
在應用自身的 didFinishLaunchingWithOptions 前,加入一個函數:

- (void)installUncaughtExceptionHandler
{
InstallUncaughtExceptionHandler();
}
最後,在 didFinishLaunchingWithOptions 中加入這一句代碼就行了:

1
[self InstallUncaughtExceptionHandler];
現在,基本上所有崩潰都能Hold住了。崩潰時將會顯示出如下的對話方塊:

這樣在崩潰時還能從容地彈出對話方塊,比起閃退來,使用者也不會覺得那麼不爽。然後在下次啟動時還可以通過郵件來發送Crash檔案到郵箱,這就看各個應用的需求了。

IOS應用之一--異常處理(UncaughtExceptionHandler)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.