ios 异常捕获发送

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

    //安装异常捕获
#ifdef DEBUG
    [NSThread detachNewThreadSelector:@selector(startCatchException) toTarget:self withObject:nil];
#endif

2,

- (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: @"a@b.com", @"a@a.com",nil];
    [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
    //添加抄送
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"e@e.com" ,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

  

转载于:https://www.cnblogs.com/zcw-ios/articles/3461518.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值