首页
文章
留言
首页
文章
留言
iOS开发之收集崩溃信息
2016 年 11 月 11 日
移动应用
Objective-C
iOS
iOS 开发中,为了统计用户的一些崩溃信息,从而对崩溃日志进行分析并修复程序,那么除了使用第三方比如友盟等来收集崩溃信息之外,也还可以自己实现收集用户崩溃信息等方法。 AppDelegate.m 文件: ```objectivec // // AppDelegate.m // 收集崩溃信息 // // Created by PengYunjing on 2015/09/07. // Copyright © 2015年 PengYunjing All rights reserved. // #import "AppDelegate.h" #define kCrashInfoKey @"kCrashInfoKey" //崩溃信息 #define kUserDefaults [NSUserDefaults standardUserDefaults] @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //收集奔溃信息 NSSetUncaughtExceptionHandler(&handler); return YES; } /** * 产生成了崩溃信息 -- > 发送崩溃信息 */ void handler(NSException *exception) { //获取崩溃信息 NSString *name = exception.name; NSString *reason = exception.reason; NSArray *callStackSymbols = exception.callStackSymbols; NSDictionary *userInfo = exception.userInfo; NSString *crashLogInfo = [NSString stringWithFormat:@"\nName: %@\nReason: %@\nCallStackSymbols: %@\nUserInfo: %@",name,reason,callStackSymbols,userInfo]; //获取手机信息 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; //系统版本 (e.g. @"8.0") NSString *strSysVersion = [[UIDevice currentDevice] systemVersion]; //软件版本 NSString *strAppVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; /* //拼接发送邮件的内容 -- > 发送邮件 NSString *urlStr = [NSString stringWithFormat:@"mailto://212969178@qq.com?subject=应用程序崩溃&body=将要发送崩溃信息给开发者,感谢您的配合!\n\n\n系统版本:%@\n软件版本:%@\n\n崩溃详情:%@",strSysVersion,strAppVersion,crashLogInfo]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; */ //拼接邮件内容 -- > 保存崩溃信息 NSString *crashStr = [NSString stringWithFormat:@"将要发送崩溃信息给开发者,感谢您的配合!\n\n\n系统版本:%@\n软件版本:%@\n\n崩溃详情:%@",strSysVersion,strAppVersion,crashLogInfo]; //保存崩溃信息到沙盒 [kUserDefaults setObject:crashStr forKey:kCrashInfoKey]; [kUserDefaults synchronize]; } ``` ViewController.m 文件: ```objectivec // // ViewController.m // 收集崩溃信息 // // Created by PengYunjing on 2015/09/07. // Copyright © 2015年 PengYunjing All rights reserved. // #import "ViewController.h" #import
#define kCrashInfoKey @"kCrashInfoKey" //崩溃信息 #define kEmailAddress @"12345678@qq.com" #define kUserDefaults [NSUserDefaults standardUserDefaults] @interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *startButton; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.startButton setTitle:@"给我来个崩溃" forState:UIControlStateNormal]; /** 判断是否要发送崩溃信息 **/ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self whetherSendCrashInfo]; }); } - (IBAction)go { @[][1]; } #pragma mark 判断是否发送崩溃信息 - (void)whetherSendCrashInfo { if ([kUserDefaults objectForKey:kCrashInfoKey]) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示!" message:@"检测到上次意外退出,为提高用户体验,您的信息很重要!是否发送崩溃信息给开发者?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"不发送" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { [kUserDefaults removeObjectForKey:kCrashInfoKey]; [kUserDefaults synchronize]; }]; UIAlertAction *send = [UIAlertAction actionWithTitle:@"发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if (![MFMailComposeViewController canSendMail]) { UIAlertView *wAlertView = [[UIAlertView alloc]initWithTitle:@"不能发送邮件!" message:@"请检查邮件设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [wAlertView show]; return; }else { [self sendCrashInfo]; } }]; [alert addAction:cancel]; [alert addAction:send]; [self presentViewController:alert animated:YES completion:nil]; } } #pragma mark - 发送崩溃信息 - (void)sendCrashInfo { // 初始化MFMailComposeViewController实例 MFMailComposeViewController *wMailViewController = [[MFMailComposeViewController alloc] init]; wMailViewController.mailComposeDelegate = self; // 设置邮件title NSString *title = @"崩溃日志"; [wMailViewController setSubject:title]; // 设置收件地址 [wMailViewController setToRecipients:[NSArray arrayWithObject:kEmailAddress]]; // 设置邮件正文内容 [wMailViewController setMessageBody:[kUserDefaults objectForKey:kCrashInfoKey] isHTML:NO]; // 模态切换弹出 [self presentViewController:wMailViewController animated:YES completion:^{ }]; } #pragma mark - MFMailComposeViewControllerDelegate - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { switch (result) { case MFMailComposeResultCancelled: // 用户取消编辑 //NSLog(@"取消了编辑"); break; case MFMailComposeResultSaved: // 用户保存邮件 //NSLog(@"保存了邮件"); break; case MFMailComposeResultSent: // 用户点击发送 //NSLog(@"已发送"); break; case MFMailComposeResultFailed: // 用户尝试保存或发送邮件失败 //NSLog(@"发送失败: %@...", [error localizedDescription]); break; } // 关闭邮件发送视图 [self dismissViewControllerAnimated:YES completion:^{ [kUserDefaults removeObjectForKey:kCrashInfoKey]; [kUserDefaults synchronize]; }]; } @end ```
0
相关文章
iOS开发之多线程
iOS开发之面向对象
iOS开发之类与类扩展
iOS开发之定时执行任务
iOS开发之WiFi传输文件
全部分类
前端
后端
运维
架构
算法
数据库
移动应用
桌面应用
程序开发
热门标签
多线程
PHP
MySQL
OpenResty
Linux
Android
Lua
Objective-C
HTML
NoSQL
C++
Nginx
iOS
Supervisor
Kafka
CentOS
Composer
Docker
GUI
Python
爬虫
Elasticsearch
MongoDB
Qt
Sphinx
Redis
JavaScript
Git
Kubernetes
CSS
Shell
macOS
热门文章
Supervisor使用总结
10种常见的软件架构模式
Redis基本使用总结
CentOS常用命令总结
iOS开发之WiFi传输文件
iOS开发之定时执行任务
Nginx常用配置说明
Composer使用总结
C/C++基础知识总结
CSS设置图片水平及垂直居中