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来实现发布前的用户测 ...
随机推荐
- 禅道——Linux服务器部署禅道
前言 2019年6月14日 22:01:24 看看时间我知道,我离猝死依然不远~ 禅道是什么 | 禅道是专业的研发项目管理软件 禅道的官网 | https://www.zentao.net/ 禅道开源 ...
- springboot在idea实现热部署
1.在pom.xml引入热部署devtools依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- 阿里云短信服务发送短信验证码(JAVA开发此功能)
开发此功能需注册阿里云账号,并开通短信服务(免费开通) 充值后,不会影响业务的正常使用!(因为发送验证类短信:1-10万范围的短信是0.045元/条).开发测试使用,充2块钱测试足够了 可参考阿里云官 ...
- 关闭ext4文件系统的日志功能
最近在帮一个研究生弄一个虚拟化环境下的基于Innodb的日志文件的读写优化的实验,实验的具体详细内容就不说了,其中有一个步骤需要将MySQL的日志文件放置在一块单独的硬盘里面,这块硬盘要么是ext2, ...
- hdu-5676 ztr loves lucky numbers(乱搞题)
题目链接: ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- RxJava 参考文档
/*************************************************************** * RxJava 参考文档 * 说明: * 最近无意中发现RxJava ...
- 【Codeforces 582A】 GCD Table
[题目链接] 点击打开链接 [算法] G中最大的数一定也是a中最大的数. G中次大的数一定也是a中次大的数. 第三.第四可能是由最大和次大的gcd产生的 那么就不难想到下面的算法: ...
- (转)Excel自定义格式详解
”G/通用格式”:以常规的数字显示,相当于”分类”列表中的”常规”选项.例:代码:”G/通用格式”.10显示为10:10.1显示为10.1. 2. “#”:数字占位符.只显有意义的零而不显示无意义的零 ...
- web项目中url-pattern改成'/'后,js、css、图片等静态资源(404)无法访问问题解决办法
感谢http://blog.csdn.net/this_super/article/details/7884383的文章 1.增加静态资源url映射 如Tomcat, Jetty, JBoss, Gl ...
- 洛谷 P3957 跳房子 —— 二分答案+单调队列优化DP
题目:https://www.luogu.org/problemnew/show/P3957 先二分一个 g,然后判断: 由于转移的范围是一个区间,也就是滑动窗口,所以单调队列优化: 可以先令队尾为 ...