第三方的了,有友盟,騰訊的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;
}
}