iOS之異常捕獲及發送

1,

 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions下添加


    //安裝異常捕獲
#ifdef DEBUG
    [NSThread detachNewThreadSelector:@selector(startCatchException) toTarget:self withObject:nil];
#endif

- (void)startCatchException
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self setCompileTime];
    //保存錯誤日誌
    [CatchExceptionHandler setDefaultHandler];

    [pool drain];
}

3,捕獲異常類

.h

@interface CatchExceptionHandler : NSObject

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

@end

.m

#import "CatchExceptionHandler.h"
#import "ABServices.h"

#define NSExceptionFile @"errorFile.txt"

@implementation CatchExceptionHandler


NSString *applicationDocumentsDirectory() {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString * documentsDirectory  =[paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:NSExceptionFile];
}

void UncaughtExceptionHandler(NSException *exception)
{
    NSArray *arr = [exception callStackSymbols];
    NSString *reason = [exception reason];
    NSString *name = [exception name];
    NSString *strTime =[ABServices getCurrentSysTime];

    NSString *strError = [NSString stringWithFormat:@"\n\n\n=============異常崩潰報告=============\n當前版本的編譯時間:\n%@\n崩潰發生的時間:\n %@\n崩潰名稱:\n%@\n崩潰原因:\n%@\n堆棧信息:\n%@", [ABServices currentCompileTime],strTime,name,reason,[arr componentsJoinedByString:@"\n"]];

    NSString *path = applicationDocumentsDirectory();
    if ([[NSFileManager defaultManager]fileExistsAtPath:path])
    {
        NSString *lasterror = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        strError  = [NSString stringWithFormat:@"%@%@", lasterror, strError];
        [lasterror release];
    }
    [strError writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];
    //添加收件人
    NSArray *toRecipients = [NSArray arrayWithObjects: @"[email protected]", @"[email protected]",nil];
    [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
    //添加抄送
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]" ,nil];
    [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
    //添加密送
    NSArray *bccRecipients = [NSArray arrayWithObjects:@"", nil];
    [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
    [mailUrl appendString:@"&subject=崩潰日誌"];
    //添加郵件內容
    [mailUrl appendString:[NSString stringWithFormat:@"&body=%@", strError]];
    NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    //#ifdef _DEBUG_LOG_
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
    //#endif
}


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

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

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