国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS開發App捕獲異常,反饋給服務器,提高用戶體驗

2019-11-14 17:55:55
字體:
來源:轉載
供稿:網友

  在我們開發的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,[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;

}

@end


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闽侯县| 潜江市| 晋中市| 宜良县| 河曲县| 班玛县| 广丰县| 长宁县| 镇宁| 阜阳市| 淳化县| 武宁县| 于都县| 丹巴县| 犍为县| 黔西县| 汪清县| 阿拉尔市| 莱阳市| 上犹县| 六枝特区| 阿拉善右旗| 昔阳县| 新巴尔虎右旗| 子洲县| 同心县| 张家港市| 台东县| 长春市| 古田县| 手游| 红桥区| 日土县| 广州市| 新乡市| 忻州市| 鱼台县| 桐梓县| 英山县| 马尔康县| 黔江区|