在我们开发的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. poj 1724 ROADS 解题报告

    题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...

  2. html5--6-24 css3前缀

    html5--6-24 css3前缀 学习要点 掌握css3前缀的使用 CSS3目前很多新增属性尚未被W3C列为标准,对这些暂时未被公布为标准的属性,各家浏览器会在属性前加上前缀词,也将其称之为浏览器 ...

  3. Android 增,删,改,查 通讯录中的联系人

    一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...

  4. Swagger测试工具

    http://www.360doc.com/content/16/0509/08/1355383_557462195.shtml

  5. 【转】构建Maven项目自动下载jar包

    原文地址:https://blog.csdn.net/gfd54gd5f46/article/details/54973954 使用Maven 自动下载jar包 右键单击项目,将项目 转换成Maven ...

  6. explain之二:Explain 结果解读与实践,分析诊断工具之二

    MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提供任何调整建议,但它能够提供重要的信息 ...

  7. NOI前总结:点分治

    点分治: 点分治的题目基本一样,都是路径计数. 其复杂度的保证是依靠 $O(n)$ 找重心的,每一次至少将问题规模减小为原先的$1/2$. 找重心我喜欢$BFS$防止爆栈. int Root(int ...

  8. 任务45:Identity MVC:注册逻辑实现

    任务45:Identity MVC:注册逻辑实现 做登陆 在注册成功直接进行登陆,使用SignIn 这里的signIn实际上是HttpContext.Signin的封装 await _signMana ...

  9. JAVA基础-面向对象07

    一.代码块 1. 含义: 就是使用大括号括起来的一段代码 格式 { 代码: } 2.静态代码块 格式 static{ 代码: } 书写位置: 直接书写在类中成员位置: 怎么执行呢? 在类加载的最后一步 ...

  10. E20180419-hm

    rectangle n. [数] 长方形,矩形; ratio n. 比例; 比,比率; 系数;  vt. 求出比值,除,使…成比例; 将(相片)按比例放大[缩小]; aspect  n. 方面; 面貌 ...