捕獲軟件異常,再次運行時發送到服務器

  在咱們開發的app中, 不可避免的, 有時候用戶使用軟件會崩潰.  咱們就須要捕獲異常, 能夠在入口類中加入相應的代碼, 能夠在每次用戶打開程序的時候, 檢查一下沙盒中是否有崩潰日誌, 若是有, 能夠發送給服務器, 方便改進軟件. 服務器

  

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

{atom

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

    self.window.backgroundColor = [UIColor whiteColor];日誌

    [self.window makeKeyAndVisible];component

    

  

    // 這裏反饋給服務器orm

 

    self.window.rootViewController = [ViewController new];ci

    return YES;開發

}string

宏定義

ExceptionHandler  捕獲異常的宏定義

#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,[callStack componentsJoinedByString:@"\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;

}

相關文章
相關標籤/搜索