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开发网络篇—搭建本地服务器 一.简单说明 说明:提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提示:提前准备好的软件 apache- ...
- iOS开发通过AFNetworking上传图片到服务器
iOS开发通过AFNetworking上传图片到服务器 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager. ...
- 巧用Ajax的beforeSend 提高用户体验--防止重复数据
巧用Ajax的beforeSend 提高用户体验 jQuery是经常使用的一个开源js框架,其中的$.ajax请求中有一个beforeSend方法,用于在向服务器发送请求前执行一些动作.具体可参考jQ ...
- 前端如何实现图片懒加载(lazyload) 提高用户体验
定义 图片懒加载又称图片延时加载.惰性加载,即在用户需要使用图片的时候加载,这样可以减少请求,节省带宽,提高页面加载速度,相对的,也能减少服务器压力. 惰性加载是程序人性化的一种体现,提高用户体验,防 ...
- iOS开发app启动原理及视图和控制器的函数调用顺序
main()函数是整个程序的入口,在程序启动之前,系统会调用exec()函数.在Unix中exec和system的不同在于,system是用shell来调用程序,相当于fork+exec+waitpi ...
- iOS开发——app审核指导方针(官网)
iOS 开发后上传到App Store审核的指导方针 ——苹果官网介绍地址 https://developer.apple.com/app-store/review/guidelines/
- iOS开发-APP测试基本流程
1. UI 测试app主要核ui与实际设计的效果图是否一致:交互方面的问题建议,可以先与产品经理确认,确认通过后,才开始让开发实施更改或优化 2. 功能测试根据软件说明或用户需求验证App的各个功能实 ...
- IOS开发之——意见反馈UITextView的使用+不能输入字符输入
@interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>@property (nonat ...
- iOS 开发 入门:使用Ad Hoc 进行用户测试
在完成iOS开发,准备进行发布之前,我们都希望App能在周围的朋友之间先进行测 试,提提意见,修改完善之后再发布到App Store上.Apple考虑到这一点,因此通过Ad Hoc来实现发布前的用户测 ...
随机推荐
- CSS animation-timing-function 属性中的 steps() 与 step-start,step-end
steps() 设置间隔参数,可以实现分步过渡 第一个参数指定了时间函数中的间隔数量(必须是正整数)第二个参数可选,接受 start 和 end 两个值,指定在每个间隔的起点或是终点发生阶跃变化,默认 ...
- 代码块、继承、this、super、final(java基础知识八)
1.代码块的概述和分类 * A:代码块概述 * 在Java中,使用{}括起来的代码被称为代码块.* B:代码块分类 * 根据其位置和声明的不同,可以分为局部代码块,构造代码块,静态代码块,同步代码块( ...
- hdu 超级楼梯 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 哦--对了,这些题读者可以直接忽略,我只是想练习一下自己薄弱的地方...... 题目意思我就不说 ...
- lucene 5的测试程序——API变动太大
package hello; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import ...
- java回调机制及其实现
1. 什么是回调函数 回调函数,顾名思义,用于回调的函数.回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用(回调)时机.回调 ...
- codeforces 691F F. Couple Cover(组合计数)
题目链接: F. Couple Cover time limit per test 3 seconds memory limit per test 512 megabytes input standa ...
- VMware Ubuntu 共享文件夹
/**************************************************************************** * VMware Ubuntu 共享文件夹 ...
- PostgreSQL新手教程
自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQL的安装和基本用法,供初次使用者上手.以下内容基于Debian操作系统,其他操作系 ...
- ol 与ul 的区别
1 <!DOCTYPE html> <html> <body> <ul> <li>咖啡</li> <li>牛奶< ...
- Python 函数的参数传递
C/C++中,传递参数的类型是可以指定的.一般来说,传递参数可以分为两种:值传递和引用传递.对于值传递,参数传递的过程中进行了复制操作,也就是说,在函数中对参数的任何改动都不会影响到传入的变量:对于引 ...