您的位置:首页 > 移动开发

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

2015-11-14 12:51 621 查看
  在我们开发的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: