// // DebugView.m // Unity-iPhone // // Created by duowan123 on 2021/12/22. // #import "DebugView.h" @implementation DebugView #pragma mark ===============================================>> 静态的初始化方法 init的时候(建立蓝牙中心管理类,设置代理) //单例静态 static DebugView* instance = nil; +(instancetype)sharedInstance{ // NSLog(@"创建单例一次 1"); return [[self alloc] init]; } + (instancetype)allocWithZone:(struct _NSZone *)zone{ // NSLog(@"创建单例一次 2"); static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [super allocWithZone:zone]; }); return instance; } - (instancetype)init{ // NSLog(@"创建单例一次 3"); static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [super init]; }); return instance; } #pragma mark ===============================================>> DEBUG -(void)inittestLabel{ if (self.testLabel!=nil){ [self.testLabel removeFromSuperview]; } if (self.uploadButton!=nil){ [self.uploadButton removeFromSuperview]; } if (self.clearnButton!=nil){ [self.clearnButton removeFromSuperview]; } self.uploadButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.uploadButton.frame = CGRectMake(30, SCREEN_HEIGHT-40-10, SCALEoefficient(60), SCALEoefficient(25)); self.uploadButton.backgroundColor = [UIColor redColor]; [self.uploadButton addTarget: self action: @selector (button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown]; [self.uploadButton addTarget: self action: @selector (button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside]; [self.uploadButton setFont: [UIFont boldSystemFontOfSize:SCALEoefficient(13)]]; [self.uploadButton setTitle:@"上传" forState:UIControlStateNormal]; [self.uploadButton addTarget:self action:@selector(uploadpPlistFile) forControlEvents:UIControlEventTouchUpInside]; self.clearnButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.clearnButton.frame = CGRectMake(30+SCALEoefficient(60)+20, SCREEN_HEIGHT-40-10, SCALEoefficient(60), SCALEoefficient(25)); self.clearnButton.backgroundColor = [UIColor redColor]; // [self.clearnButton addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown]; // [self.clearnButton addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside]; [self.clearnButton setFont: [UIFont boldSystemFontOfSize:SCALEoefficient(13)]]; [self.clearnButton setTitle:@"清理缓存" forState:UIControlStateNormal]; [self.clearnButton addTarget:self action:@selector(clearCacheWithFilePath) forControlEvents:UIControlEventTouchUpInside]; self.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCALEoefficient(20), -20, [UIScreen mainScreen].bounds.size.width-100, SCALEoefficient(140))]; self.testLabel.textColor = [UIColor redColor]; self.testLabel.font = [UIFont boldSystemFontOfSize:SCALEoefficient(14)]; self.testLabel.backgroundColor = [UIColor clearColor]; self.testLabel.textAlignment = NSTextAlignmentLeft; self.testLabel.numberOfLines = 0; // if ([UIApplication sharedApplication].keyWindow != nil){ [[UIApplication sharedApplication].keyWindow addSubview:self.uploadButton]; [[UIApplication sharedApplication].keyWindow addSubview:self.clearnButton]; [[UIApplication sharedApplication].keyWindow addSubview:self.testLabel]; } } // button普通状态下的背景色 - (void )button1BackGroundNormal:(UIButton *)sender { sender.backgroundColor = [UIColor redColor]; } // button高亮状态下的背景色 - (void )button1BackGroundHighlighted:(UIButton *)sender { sender.backgroundColor = [UIColor orangeColor]; } #pragma mark ===============================================>> 存储和上传数据 //清理缓存 - (void)clearCacheWithFilePath{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //获取完整路径 NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:JBSDKLog];//这里就是你将要存储的沙盒路径(.plist文件,名字自定义) NSError *error = nil; //删除子文件夹 [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]; if (error){ NSLog(@"清理缓存失败"); [PopupView showCusHUDA:@"清理失败"]; }else{ NSLog(@"清理缓存成功"); [PopupView showCusHUDA:@"清理成功"]; } } //数据写入沙河 -(void)writeFileToplist{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //获取完整路径 NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:JBSDKLog];//这里就是你将要存储的沙盒路径(.plist文件,名字自定义) if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){//plistPath这个文件\文件夹是否存在 NSLog(@"-------文件不存在,写入文件----------"); NSError *error; if([self.tempStepString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]){ NSLog(@"------写入文件------success"); }else{ NSLog(@"------写入文件------fail,error==%@",error); }; }else{ // NSLog(@"-------文件存在,追加文件----------"); NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath]; [fileHandle seekToEndOfFile]; //将节点跳到文件的末尾 NSData* stringData = [self.tempStepString dataUsingEncoding:NSUTF8StringEncoding]; [fileHandle writeData:stringData]; //追加写入数据 [fileHandle closeFile]; } } -(void)uploadpPlistFile{ //这个方法获取出的结果是一个数组.因为有可以搜索到多个路径. NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //在这里,我们指定搜索的是Cache目录,所以结果只有一个,取出Cache目录 NSString *documentsDirectory = array[0]; // NSLog(@"%@",documentsDirectory); //拼接文件路径 NSString *filePathName = [documentsDirectory stringByAppendingPathComponent:JBSDKLog]; //如果保存的是一个数组.那就通过数组从文件当中加载. NSString *string = [NSString stringWithContentsOfFile:filePathName encoding:NSUTF8StringEncoding error:nil]; // NSLog(@"%@",string); //4.设置请求体 NSData *upData = [string dataUsingEncoding:NSUTF8StringEncoding]; // AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer.timeoutInterval=15; [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/html",@"application/json",@"text/javascript",@"image/jpeg",@"image/png",@"application/octet-stream",@"application/x-www-form-urlencoded", nil]; // NSLog(@"请求URL ===================>> %@",urlString); NSLog(@"******************* 请求头参数 = %@ ***************************",manager.requestSerializer.HTTPRequestHeaders); NSString * gameType; NSString * bundleIdentifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]; if ([bundleIdentifier isEqualToString:@"com.Oujia.Run"]){ gameType = @"run"; }else if ([bundleIdentifier isEqualToString:@"com.Oujia.Dance"]){ gameType = @"Dance"; } AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:nil]; [sessionManager POST:@"http://172.16.14.127:8080/upload" parameters:nil headers:nil constructingBodyWithBlock:^(id _Nonnull formData){ [formData appendPartWithFileData:upData name:@"file" fileName:[NSString stringWithFormat:@"ios_%@_%@.txt",gameType,[self getNowTimeTimestamp]] mimeType:@"text/plain"]; }progress:^(NSProgress * _Nonnull uploadProgress){ NSLog(@"测试数据 上传进度 = %@",uploadProgress); }success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject){ NSLog(@"测试数据 上传成功"); }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){ for (NSString * key in error.userInfo){ if ([key isEqualToString:@"com.alamofire.serialization.response.error.data"]){ id errorObject = [NSJSONSerialization JSONObjectWithData:error.userInfo[key] options:1 error:nil]; NSLog(@"测试数据 上传失败 ===>> %@",errorObject); if (errorObject !=nil && [errorObject isKindOfClass:[NSDictionary class]]){ NSString * result = errorObject[@"result"]; if ([result intValue] ==1){ [PopupView showCusHUDA:@"测试数据 上传成功"]; } } } } }]; } //获取当前时间戳有两种方法(以秒为单位) -(NSString *)getNowTimeTimestamp{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 //设置时区,这个对于时间的处理有时很重要 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [formatter setTimeZone:timeZone]; NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式 NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]]; return timeSp; } @end