在我们开发的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捕获异常, 反馈给服务器, 提高用户体验的更多相关文章

  1. iOS开发网络篇—搭建本地服务器

    iOS开发网络篇—搭建本地服务器 一.简单说明 说明:提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提示:提前准备好的软件 apache- ...

  2. iOS开发通过AFNetworking上传图片到服务器

    iOS开发通过AFNetworking上传图片到服务器 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager. ...

  3. 巧用Ajax的beforeSend 提高用户体验--防止重复数据

    巧用Ajax的beforeSend 提高用户体验 jQuery是经常使用的一个开源js框架,其中的$.ajax请求中有一个beforeSend方法,用于在向服务器发送请求前执行一些动作.具体可参考jQ ...

  4. 前端如何实现图片懒加载(lazyload) 提高用户体验

    定义 图片懒加载又称图片延时加载.惰性加载,即在用户需要使用图片的时候加载,这样可以减少请求,节省带宽,提高页面加载速度,相对的,也能减少服务器压力. 惰性加载是程序人性化的一种体现,提高用户体验,防 ...

  5. iOS开发app启动原理及视图和控制器的函数调用顺序

    main()函数是整个程序的入口,在程序启动之前,系统会调用exec()函数.在Unix中exec和system的不同在于,system是用shell来调用程序,相当于fork+exec+waitpi ...

  6. iOS开发——app审核指导方针(官网)

    iOS 开发后上传到App Store审核的指导方针 ——苹果官网介绍地址 https://developer.apple.com/app-store/review/guidelines/

  7. iOS开发-APP测试基本流程

    1. UI 测试app主要核ui与实际设计的效果图是否一致:交互方面的问题建议,可以先与产品经理确认,确认通过后,才开始让开发实施更改或优化 2. 功能测试根据软件说明或用户需求验证App的各个功能实 ...

  8. IOS开发之——意见反馈UITextView的使用+不能输入字符输入

    @interface DMFeedbackViewController ()<UITextViewDelegate,UIAlertViewDelegate>@property (nonat ...

  9. iOS 开发 入门:使用Ad Hoc 进行用户测试

    在完成iOS开发,准备进行发布之前,我们都希望App能在周围的朋友之间先进行测 试,提提意见,修改完善之后再发布到App Store上.Apple考虑到这一点,因此通过Ad Hoc来实现发布前的用户测 ...

随机推荐

  1. 在织梦dedecms中实现“文章标题-栏目名称-网站名”导航

    本文介绍了在dedecms中,实现文章标题-栏目名称-网站名 导航的方法,有需要的朋友参考下. 在dedecms中实现“文章标题-栏目名称-网站名”导航的方法.   第一种: 在/include/in ...

  2. linux--memcache的安装和使用(转)

    memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.据说官方所说,其用户包括twitter.digg.flickr等,都是些互联网大腕呀.目前用memca ...

  3. 洛谷P3195 [HNOI2008]玩具装箱TOY——斜率优化DP

    题目:https://www.luogu.org/problemnew/show/P3195 第一次用斜率优化...其实还是有点云里雾里的: 网上的题解都很详细,我的理解就是通过把式子变形,假定一个最 ...

  4. 从MyEclipse到IntelliJ IDEA

    如何做到全键盘操作呢? 1.自定义快捷键实现全屏操作 你可以设置自定义快捷键进入全屏操作,并实现各个窗口之间的切换.这样,你就可以告别小窗口的时代,体验全屏显示的效果了!(相信有过多年开发经验的你一定 ...

  5. 使用git rebase合并多次commit

    使用git rebase合并多次commit 聊下 git rebase -i

  6. Codeforces 702B【二分】

    题意: 给一个a数组,输出有多少对相加是等于2^x的.1<=a[i]<=1e9,n<=1e5 思路: a[i]+a[j]=2^x 对于每个a[i],枚举x,然后二分查找a[j]; p ...

  7. hdoj1166【线段树】

    单点更新+区间求和 不多说,直接上渣code--- #include<cstdio> #include<iostream> #include<string.h> # ...

  8. python __builtins__ filter类 (24)

    24.'filter', 用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表.该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True ...

  9. spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】

    多串LCS很适合SA但是我要学SAM 对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c ...

  10. c#删除指定文件夹中今天之前的文件

    1.说明 使用Directory类对指定文件夹下的今天或者更早日期之前的文件进行删除.原文链接:http://www.cnblogs.com/lengzhan/p/6423943.html 2.代码 ...