ios 捕抓crash_iOS AppCrashHandler全局异常捕获和上传

全局异常捕获对App开发和日常产品维护具有重要的意义,我们不可能在程序开发中做的极致,因此我们需要用户终端反馈异常信息。

在iOS中捕获全局异常需要有以下几种条件

1.实现全局钩子(默认异常处理句柄)

2.收集手机基础参数信息

3.打印异常堆栈信息

主要代码如下:

AppCatchExceptionHandler.h

@interface AppCatchExceptionHandler : NSObject

+ (void)setDefaultHandler;

+ (NSUncaughtExceptionHandler *)getUncaughtExceptionHandler;

@end

AppCatchExceptionHandler.m

#import 

#import "CatchExceptionHandler.h"

#define AppExceptionFile @"log_ErrorFile_%@.txt"

@implementation AppCatchExceptionHandler

NSString *applicationDocumentsDirectory() {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString * documentsDirectory  =[paths objectAtIndex:0];

NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

NSTimeInterval timeinterval =[dat timeIntervalSince1970]*1000;

NSString *timeString = [NSString stringWithFormat:@"%0.0f", timeinterval];//转为字符型

NSString *logFilename = = [NSString stringWidthFormat:AppExceptionFile,timeString];

return [documentsDirectory stringByAppendingPathComponent:logFilename ];

}

void UncaughtExceptionHandler(NSException *exception)

{

NSArray *arr = [exception callStackSymbols];

NSString *reason = [exception reason];

NSString *name = [exception name];

NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

NSTimeInterval timeinterval =[dat timeIntervalSince1970]*1000;

NSString *timeString = [NSString stringWithFormat:@"%0.0f", timeinterval];//转为字符型

NSString *strTime =timeString;

NSString *strError = [NSString stringWithFormat:@"\n\n\n=============异常崩溃报告=============\n当前版本的编译时间:\n%@\n崩溃发生的时间:\n %@\n崩溃名称:\n%@\n崩溃原因:\n%@\n堆栈信息:\n%@",timeString ,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*)getUncaughtExceptionHandler

{

return NSGetUncaughtExceptionHandler();

}

@end

使用方式

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

{

[NSThread detachNewThreadSelector:@selector(setDefaultHandler) toTarget:AppCatchExceptionHandler withObject:nil];

return YES;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值