星空网 > 软件开发 > 操作系统

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,[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




原标题:iOS 开发App捕获异常, 反馈给服务器, 提高用户体验

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流