1. 程式人生 > >iOS 崩潰日誌 收集與傳送伺服器

iOS 崩潰日誌 收集與傳送伺服器


iOS開發中我們會遇到程式丟擲異常退出的情況,如果是在除錯的過程中,異常的資訊是一目瞭然,我們可以很快的定位異常的位置並解決問題。那麼當應用已經打包,iPhone裝置通過ipa的包安裝應用後,在使用過程發現crash,那麼如何獲取crash日誌呢?對於保密性要求不高的程式來說,也可以選擇各種一條龍Crash統計產品,如 Crashlytics,Hockeyapp ,友盟,Bugly 等等,不過IOS SDK中提供了一個現成的函式 NSSetUncaughtExceptionHandler 用來做異常處理
利用NSSetUncaughtExceptionHandler,當程式異常退出的時候,可以先進行處理,然後做一些自定義的動作,並通知開發者,是大多數軟體都選擇的方法。下面就介紹如何在iOS中實現:


首先建立一個MyUncaughtExceptionHandler類 (名字可以自己起)實現崩潰時呼叫的函式,下面我貼上我程式中的完整程式碼,你們需要用的時候可以直接複製就可以.

#import <Foundation/Foundation.h>
// 崩潰日誌
@interface MyUncaughtExceptionHandler : NSObject

+ (void)setDefaultHandler;
+ (NSUncaughtExceptionHandler *)getHandler;
+ (void)TakeException:(NSException *) exception;

end

#import "MyUncaughtExceptionHandler.h"
#import "AFNetworking.h"
// 沙盒的地址
NSString * applicationDocumentsDirectory() {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

// 崩潰時的回撥函式
void UncaughtExceptionHandler(NSException * exception) {
    NSArray
* arr = [exception callStackSymbols]; NSString * reason = [exception reason]; // // 崩潰的原因 可以有崩潰的原因(陣列越界,字典nil,呼叫未知方法...) 崩潰的控制器以及方法 NSString * name = [exception name]; NSString * url = [NSString stringWithFormat:@"========異常錯誤報告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]]; NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"]; // 將一個txt檔案寫入沙盒 [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; } @implementation MyUncaughtExceptionHandler // 沙盒地址 - (NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; } + (void)setDefaultHandler { NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler); } + (NSUncaughtExceptionHandler *)getHandler { return NSGetUncaughtExceptionHandler(); } + (void)TakeException:(NSException *)exception { NSArray * arr = [exception callStackSymbols]; NSString * reason = [exception reason]; NSString * name = [exception name]; NSString * url = [NSString stringWithFormat:@"========異常錯誤報告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[arr componentsJoinedByString:@"\n"]]; NSString * path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"]; [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; } @end


在appledelegate匯入標頭檔案加上一個異常捕獲監聽,用來處理程式崩潰時的回撥動作 在這裡也要判斷一下之前有沒有崩潰日誌 如果有傳送給伺服器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   #pragma mark -- 崩潰日誌
    [MyUncaughtExceptionHandler setDefaultHandler];
    // 傳送崩潰日誌
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dataPath = [path stringByAppendingPathComponent:@"Exception.txt"];
    NSData *data = [NSData dataWithContentsOfFile:dataPath];
    if (data != nil) {
         [self sendExceptionLogWithData:data path:dataPath];
    }

    return YES;
}

#pragma mark -- 傳送崩潰日誌
- (void)sendExceptionLogWithData:(NSData *)data path:(NSString *)path {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5.0f;
    //告訴AFN,支援接受 text/xml 的資料
    [AFJSONResponseSerializer serializer].acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString *urlString = @"後臺地址";

    [manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:data name:@"file" fileName:@"Exception.txt" mimeType:@"txt"];
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // 刪除檔案
        NSFileManager *fileManger = [NSFileManager defaultManager];
        [fileManger removeItemAtPath:path error:nil];

    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {


    }];

}

測試:可以隨便建一個控制器 弄一個數組越界測試一下~ 過兩天我把demo傳上去 給個地址再發布更新吧~