在我们开发的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. 【转】Java数字抽奖游戏核心代码

    1. [代码][Java]代码    package com.luiszhang.test; import java.util.Arrays; /** * NumberLotteryGame * 一个 ...

  2. CollapsingToolbarLayout 收缩显示tilte

    final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id. ...

  3. HTTPS站点搭建教程:Win7/Windows Server 2008R2

    本文将由笔者为各位读者介绍在win7/windows server 2008R2环境下使用SSL加密协议建立WWW站点的全过程:https SSL证书安装的搭建以及本地测试环境. 要想成功架设SSL安 ...

  4. 【前端】CentOS 7 系列教程之三: 搭建 git 服务器

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_3.html 上一篇我们安装好了git,这一篇我们搭建git服务器 创建一个用户组 groupadd g ...

  5. cocos2dx常见32种场景切换动画

    bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init( ...

  6. css3 appearance 改变元素外观

    h5 input标签的默认样式去除: 去掉date类型<input>框的叉叉: ::-webkit-clear-button { -webkit-appearance: none; } 去 ...

  7. UI:UICollectionView

    #import "ViewController.h" #import "HeaderView.h" #import "FooterView.h&quo ...

  8. Task用法

    转: https://www.cnblogs.com/wyy1234/p/9172467.html

  9. 6-6 Haar特征3

    B区域是包含AB这样两部分的.C区域是包含A和C这样两部分的.B区域和C区域它描述的是一个横条和一个竖条.D区域是四个方块之和. #haar 1 什么是haar? 特征 = 像素 运算 ->结果 ...

  10. Python网络爬虫之BeautifulSoup模块

    一.介绍: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮 ...