收集崩溃信息
AppDelegate.m:
// // AppDelegate.m // 收集崩溃信息 // // Created by PengYunjing on 2016/11/5. // Copyright © 2016年 彭运京. 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:
// // ViewController.m // 收集崩溃信息 // // Created by PengYunjing on 2016/11/5. // Copyright © 2016年 彭运京. All rights reserved. // #import "ViewController.h" #import <MessageUI/MessageUI.h> #define kCrashInfoKey @"kCrashInfoKey" //崩溃信息 #define kEmailAddress @"212969178@qq.com" #define kUserDefaults [NSUserDefaults standardUserDefaults] @interface ViewController () <MFMailComposeViewControllerDelegate> @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]; }]; }