iOS之 APP异常捕获反馈给服务器

在我们开发的app中, 不可避免的, 有时候用户使用软件会崩溃.  我们就需要捕获异常, 可以在入口类中加入相应的代码, 可以在每次用户打开程序的时候, 检查一下沙盒中是否有崩溃日志, 如果有, 可以发送给服务器, 方便改进软件. 

  

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

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    ExceptionHandler  捕获异常的宏定义

    // 这里反馈给服务器

    self.window.rootViewController = [ViewController new];

    return YES;

}

宏定义

#define ExceptionHandler [ZYExceptionHandler caughtExceptionHandler];

 

#import "ZYExceptionHandler.h"

#include <libkern/OSAtomic.h>

#include <execinfo.h>

@implementation ZYExceptionHandler

 

+ (void)caughtExceptionHandler{

    //指定crash的处理方法。

    NSSetUncaughtExceptionHandler(& UncaughtExceptionHandler);

}

 

+ (void)fileCreate{

    NSString *path = [ZYExceptionHandler exceptionPath];

    NSFileManager *manager =[NSFileManager defaultManager];

    //文件不存在时创建

    if (![manager fileExistsAtPath:path])

    {

        NSString *dateString = [ZYExceptionHandler currentTime];

        NSString *logStr = [NSString stringWithFormat:@"================\n文件创建时间:%@\n================",dateString];

        NSData *data = [logStr dataUsingEncoding:NSUTF8StringEncoding];

        

        [data writeToFile:path atomically:YES];

 

    }

}

 

void UncaughtExceptionHandler(NSException *exception) {

    /**

     *  获取异常崩溃信息

     */

    //在这里创建一个接受crash的文件

    [ZYExceptionHandler fileCreate];

 

    NSArray *callStack = [exception callStackSymbols];

    NSString *reason = [exception reason];

    NSString *name = [exception name];

    

    NSString *dateString = [ZYExceptionHandler currentTime];

 

    NSString *systemName = [[UIDevice currentDevice] systemName];

    

    NSString *strModel = [[UIDevice currentDevice] model];

 

    NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];

    NSString *bundleIdentifier = infoDict[@"CFBundleIdentifier"];

    NSString* versionNum = [infoDict objectForKey:@"CFBundleShortVersionString"];

    

    NSString *content = [NSString stringWithFormat:@"\n\n\n========异常错误报告========\n错误时间:%@ 系统:%@设备:%@\n当前版本:%@ 当前唯一标示符:%@\n\n错误名称:%@\n错误原因:\n%@\ncallStackSymbols:\n%@\n\n========异常错误结束========\n",dateString,systemName,strModel,versionNum,bundleIdentifier,name,reason,[callStackcomponentsJoinedByString:@"\n"]];

 

 

    NSString *path = [ZYExceptionHandler exceptionPath];

 

    NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:path];

    //找到并定位到outFile的末尾位置(在此后追加文件)

    [outFile seekToEndOfFile];

    

    [outFile writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];

    //关闭读写文件

    [outFile closeFile];

 

    

}

+ (NSString *)exceptionPath{

    

    NSLog(@"----->>>%@",NSHomeDirectory());

    

    NSString *documents = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];

    NSString *path = [documents stringByAppendingPathComponent:@"exceptionHandler.txt"];

    

    return path;

 

}

+ (NSString *)currentTime{

    NSDate *date = [NSDate date];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];

    NSString *dateString = [formatter stringFromDate:date];

    return dateString;

 

}

//获取调用堆栈

+ (NSArray *)backtrace

{

    void* callstack[128];

    int frames = backtrace(callstack, 128);

    char **strs = backtrace_symbols(callstack,frames);

    

    NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];

    for (int i=0;i<frames;i++)

    {

        [backtrace addObject:[NSString stringWithUTF8String:strs[i]]];

    }

    free(strs);

    

    return backtrace;

}

@end

 

转载于:https://www.cnblogs.com/chengxiaoyu/p/4969068.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值