iOS已發佈應用中對異常信息捕獲和處理

iOS已發佈應用中對異常信息捕獲和處理

 

 

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

 

iOS提供了異常發生的處理API,我們在程序啓動的時候可以添加這樣的Handler,這樣的程序發生異常的時候就可以對這一部分的信息進行必要的處理,適時的反饋給開發者。

 

不足的地方是,並不是所有的程序崩潰都是由於發生可以捕捉的異常的,有些時候是因爲內存等一些其他的錯誤導致程序的崩潰,這樣的信息是不在這裏體現的。

 

我做了一個簡單的類,進行很基本的操作,可以添加和獲取Handler,捕獲到異常後將信息寫入到app的Documens下的Exception.txt中。

 

其實還有很多的處理的辦法。

比如可以在程序下一次起來的時候讀取這個異常文件發生到服務端。

或者直接就是在處理代碼中用openurl的方式(mailto:)調用發送郵件的方式,將異常信息直接變成郵件發送到指定地址。

 

以下是完整的代碼實現。

 

使用場景示例:

 

#pragma mark -

#pragma mark Application lifecycle

 

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

   

    // Override point for customization after application launch.

    

    [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:

  CoreFoundation                      0x02393919 __exceptionPreprocess + 185

  libobjc.A.dylib                     0x024e15de objc_exception_throw + 47

  CoreFoundation                      0x0238958c -[__NSArrayI objectAtIndex:] + 236

  UncaughtE                           0x000022e8 -[UncaughtEAppDelegate application:didFinishLaunchingWithOptions:] + 157

  UIKit                               0x002b8543 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163

  UIKit                               0x002ba9a1 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346

  UIKit                               0x002c4452 -[UIApplication handleEvent:withNewEvent:] + 1958

  UIKit                               0x002bd074 -[UIApplication sendEvent:] + 71

  UIKit                               0x002c1ac4 _UIApplicationHandleEvent + 7495

  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


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章