iOS OC应用异常捕获,崩溃退出前返回信息给后台

第三方的了,有友盟,腾讯的bugly

 

查了一下网上类似的代码很多,在借鉴前辈的代码后,组合了一下: 

1、捕获异常信息

2、获得当前日期,版本,系统

3、获得出bug的视图控制器转为字符串

4、将前3条信息,同步上传反馈给后台---应用out了

 

代码如下:

//在 APPDelegate🀄️
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ///////////////捕获异常消息捕获函数 //注册消息处理函数的处理方法 NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler); } #pragma mark 收集异常,上传 void UncaughtExceptionHandler(NSException *exception) { //异常信息 NSArray *callStack = [exception callStackSymbols]; NSString *reason = [exception reason]; NSString *name = [exception name]; //日期 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSString * dateStr = [formatter stringFromDate:[NSDate date]]; //获取崩溃界面 UIViewController * viewNow = [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; //class 转字符串 NSString * nowview = NSStringFromClass([viewNow class]); // 用户信息 NSDictionary * diceuserport= [[NSUserDefaults standardUserDefaults]objectForKey:@"useruidport" ]; //app版本 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *appCurVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; //iOS系统 NSString * devicetext = [NSString stringWithFormat:@"%f",[[[UIDevice currentDevice] systemVersion] floatValue]]; //综合信息 NSString *content = [NSString stringWithFormat:@"\n日期:%@ \nuid:%@ \n端口:%@ \nAPP版本:%@ \n系统版本:%@ \n错误:%@ \n视图控制器:%@ \n错误原因:%@ \n崩溃所在:%@ ",dateStr,diceuserport[@"uid"],diceuserport[@"port"],appCurVersion,devicetext,name,nowview,reason,[callStack componentsJoinedByString:@"\n"]]; //同步方法上传服务器 (试了试异步了,还没上传就崩了) // 创建URL对象 NSURL *url =[NSURL URLWithString:YJFKPresentUrlStr]; NSMutableURLRequest *resuest =[NSMutableURLRequest requestWithURL:url]; [resuest setHTTPMethod:@"post"]; [resuest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [resuest setValue:@"application/json" forHTTPHeaderField:@"Accept"]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"uid":diceuserport[@"uid"],@"type":@"应用崩溃",@"content":content,@"contact_way":@"App-反馈"} options:NSJSONWritingPrettyPrinted error:nil]; NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData]; resuest.HTTPBody = tempJsonData; //4 创建响应对象 NSURLResponse *response = nil; //5 创建连接对象 NSError *error; NSData *data = [NSURLConnection sendSynchronousRequest:resuest returningResponse:&response error:&error]; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; //反馈结果 NSLog(@"%@",dict); }



-(UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController


{


    if ([rootViewController isKindOfClass:[UITabBarController class]]) {


        UITabBarController *tabBarController = (UITabBarController *)rootViewController;


        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];


    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {


        UINavigationController* navigationController = (UINavigationController*)rootViewController;


        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];


    } else if (rootViewController.presentedViewController) {


        UIViewController* presentedViewController = rootViewController.presentedViewController;


        return [self topViewControllerWithRootViewController: presentedViewController];


    } else {


        return rootViewController;


    }


}



 

posted @ 2017-09-26 17:33  徐家汇  阅读(1812)  评论(0编辑  收藏  举报