1.iOS
在程序的开发中我们难免会遇到崩溃的问题。然后在用户体验的时候,我们如何来防止崩溃的发生呢,并将我们的崩溃原因发送给开发者来处理它。
来看一个例子
NSString *str = @"523"; arr = @[@"sdad", @"dwada", @"ffwwra"]; [str substringFromIndex:111];
@try { NSString *str = @"523"; arr = @[@"sdad", @"dwada", @"ffwwra"]; [str substringFromIndex:111]; } @catch (NSException *exception) { NSLog(@"%s %@", __FUNCTION__, exception); } @finally { //如何来对待这个异常处理 NSLog(@"tryTwo - 我一定会执行"); }
怎么样来实现发送给开发者邮件呢?
在iOS中发送邮件是很简单的。
封装来如下方法
// // CRASHBUG.h // try处理异常 // // Created by 黄权浩 on 15-1-24. // Copyright (c) 2015年 黄权浩. All rights reserved. // #import <Foundation/Foundation.h> @interface CRASHBUG : NSObject + (void)sendBug:(NSString *)bug interface:(NSException *)interfaceinfo; @end
// // CRASHBUG.m // try处理异常 // // Created by 黄权浩 on 15-1-24. // Copyright (c) 2015年 黄权浩. All rights reserved. // #import "CRASHBUG.h" #import "AppDelegate.h" @implementation CRASHBUG + (void)sendBug:(NSString *)bug interface:(NSException *)interfaceinfo { NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; long long int date = (long long int)time; NSDate *dd = [NSDate dateWithTimeIntervalSince1970:date]; NSString *crashLogInfo = [NSString stringWithFormat:@"exception type : %@ \n crash reason : %@ \n call stack time : %@", interfaceinfo, bug, dd]; NSString *urlStr = [NSString stringWithFormat:@"mailto://xxxxxxxxx@xx.com?subject=bug report&body=Thank you for your cooperation!""Error Details:%@",crashLogInfo]; NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url]; } @end然后这么来使用
// // ViewController.m // try处理异常 // // Created by 黄权浩 on 15-1-24. // Copyright (c) 2015年 黄权浩. All rights reserved. // #import "ViewController.h" #import "CRASHBUG.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. @try { NSString *str = @"523"; [str substringFromIndex:111]; } @catch (NSException *exception) { NSLog(@"%s %@", __FUNCTION__, exception); /** * 把异常崩溃信息发送至开发者邮件 */ [CRASHBUG sendBug:@"字符串的类型转化" interface:exception]; } @finally { //如何来对待这个异常处理 NSLog(@"tryTwo - 我一定会执行"); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
原文地址:http://blog.csdn.net/quanhaoh/article/details/43083649