DebugView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // DebugView.m
  3. // Unity-iPhone
  4. //
  5. // Created by duowan123 on 2021/12/22.
  6. //
  7. #import "DebugView.h"
  8. @implementation DebugView
  9. #pragma mark ===============================================>> 静态的初始化方法 init的时候(建立蓝牙中心管理类,设置代理)
  10. //单例静态
  11. static DebugView* instance = nil;
  12. +(instancetype)sharedInstance{
  13. // NSLog(@"创建单例一次 1");
  14. return [[self alloc] init];
  15. }
  16. + (instancetype)allocWithZone:(struct _NSZone *)zone{
  17. // NSLog(@"创建单例一次 2");
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. instance = [super allocWithZone:zone];
  21. });
  22. return instance;
  23. }
  24. - (instancetype)init{
  25. // NSLog(@"创建单例一次 3");
  26. static dispatch_once_t onceToken;
  27. dispatch_once(&onceToken, ^{
  28. instance = [super init];
  29. });
  30. return instance;
  31. }
  32. #pragma mark ===============================================>> DEBUG
  33. -(void)inittestLabel{
  34. if (self.testLabel!=nil){
  35. [self.testLabel removeFromSuperview];
  36. }
  37. if (self.uploadButton!=nil){
  38. [self.uploadButton removeFromSuperview];
  39. }
  40. if (self.clearnButton!=nil){
  41. [self.clearnButton removeFromSuperview];
  42. }
  43. self.uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
  44. self.uploadButton.frame = CGRectMake(30, SCREEN_HEIGHT-40-10, SCALEoefficient(60), SCALEoefficient(25));
  45. self.uploadButton.backgroundColor = [UIColor redColor];
  46. [self.uploadButton addTarget: self action: @selector (button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
  47. [self.uploadButton addTarget: self action: @selector (button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
  48. [self.uploadButton setFont: [UIFont boldSystemFontOfSize:SCALEoefficient(13)]];
  49. [self.uploadButton setTitle:@"上传" forState:UIControlStateNormal];
  50. [self.uploadButton addTarget:self action:@selector(uploadpPlistFile) forControlEvents:UIControlEventTouchUpInside];
  51. self.clearnButton = [UIButton buttonWithType:UIButtonTypeCustom];
  52. self.clearnButton.frame = CGRectMake(30+SCALEoefficient(60)+20, SCREEN_HEIGHT-40-10, SCALEoefficient(60), SCALEoefficient(25));
  53. self.clearnButton.backgroundColor = [UIColor redColor];
  54. // [self.clearnButton addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
  55. // [self.clearnButton addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
  56. [self.clearnButton setFont: [UIFont boldSystemFontOfSize:SCALEoefficient(13)]];
  57. [self.clearnButton setTitle:@"清理缓存" forState:UIControlStateNormal];
  58. [self.clearnButton addTarget:self action:@selector(clearCacheWithFilePath) forControlEvents:UIControlEventTouchUpInside];
  59. self.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCALEoefficient(20), -20, [UIScreen mainScreen].bounds.size.width-100, SCALEoefficient(140))];
  60. self.testLabel.textColor = [UIColor redColor];
  61. self.testLabel.font = [UIFont boldSystemFontOfSize:SCALEoefficient(14)];
  62. self.testLabel.backgroundColor = [UIColor clearColor];
  63. self.testLabel.textAlignment = NSTextAlignmentLeft;
  64. self.testLabel.numberOfLines = 0;
  65. //
  66. if ([UIApplication sharedApplication].keyWindow != nil){
  67. [[UIApplication sharedApplication].keyWindow addSubview:self.uploadButton];
  68. [[UIApplication sharedApplication].keyWindow addSubview:self.clearnButton];
  69. [[UIApplication sharedApplication].keyWindow addSubview:self.testLabel];
  70. }
  71. }
  72. // button普通状态下的背景色
  73. - (void )button1BackGroundNormal:(UIButton *)sender
  74. {
  75. sender.backgroundColor = [UIColor redColor];
  76. }
  77. // button高亮状态下的背景色
  78. - (void )button1BackGroundHighlighted:(UIButton *)sender
  79. {
  80. sender.backgroundColor = [UIColor orangeColor];
  81. }
  82. #pragma mark ===============================================>> 存储和上传数据
  83. //清理缓存
  84. - (void)clearCacheWithFilePath{
  85. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  86. //获取完整路径
  87. NSString *documentsDirectory = [paths objectAtIndex:0];
  88. NSString *filePath = [documentsDirectory stringByAppendingPathComponent:JBSDKLog];//这里就是你将要存储的沙盒路径(.plist文件,名字自定义)
  89. NSError *error = nil;
  90. //删除子文件夹
  91. [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
  92. if (error){
  93. NSLog(@"清理缓存失败");
  94. [PopupView showCusHUDA:@"清理失败"];
  95. }else{
  96. NSLog(@"清理缓存成功");
  97. [PopupView showCusHUDA:@"清理成功"];
  98. }
  99. }
  100. //数据写入沙河
  101. -(void)writeFileToplist{
  102. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  103. //获取完整路径
  104. NSString *documentsDirectory = [paths objectAtIndex:0];
  105. NSString *filePath = [documentsDirectory stringByAppendingPathComponent:JBSDKLog];//这里就是你将要存储的沙盒路径(.plist文件,名字自定义)
  106. if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){//plistPath这个文件\文件夹是否存在
  107. NSLog(@"-------文件不存在,写入文件----------");
  108. NSError *error;
  109. if([self.tempStepString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]){
  110. NSLog(@"------写入文件------success");
  111. }else{
  112. NSLog(@"------写入文件------fail,error==%@",error);
  113. };
  114. }else{
  115. // NSLog(@"-------文件存在,追加文件----------");
  116. NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
  117. [fileHandle seekToEndOfFile]; //将节点跳到文件的末尾
  118. NSData* stringData = [self.tempStepString dataUsingEncoding:NSUTF8StringEncoding];
  119. [fileHandle writeData:stringData]; //追加写入数据
  120. [fileHandle closeFile];
  121. }
  122. }
  123. -(void)uploadpPlistFile{
  124. //这个方法获取出的结果是一个数组.因为有可以搜索到多个路径.
  125. NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  126. //在这里,我们指定搜索的是Cache目录,所以结果只有一个,取出Cache目录
  127. NSString *documentsDirectory = array[0];
  128. // NSLog(@"%@",documentsDirectory);
  129. //拼接文件路径
  130. NSString *filePathName = [documentsDirectory stringByAppendingPathComponent:JBSDKLog];
  131. //如果保存的是一个数组.那就通过数组从文件当中加载.
  132. NSString *string = [NSString stringWithContentsOfFile:filePathName encoding:NSUTF8StringEncoding error:nil];
  133. // NSLog(@"%@",string);
  134. //4.设置请求体
  135. NSData *upData = [string dataUsingEncoding:NSUTF8StringEncoding];
  136. //
  137. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  138. manager.requestSerializer.timeoutInterval=15;
  139. [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
  140. manager.requestSerializer = [AFJSONRequestSerializer serializer];
  141. 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];
  142. // NSLog(@"请求URL ===================>> %@",urlString);
  143. NSLog(@"******************* 请求头参数 = %@ ***************************",manager.requestSerializer.HTTPRequestHeaders);
  144. NSString * gameType;
  145. NSString * bundleIdentifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
  146. if ([bundleIdentifier isEqualToString:@"com.Oujia.Run"]){
  147. gameType = @"run";
  148. }else if ([bundleIdentifier isEqualToString:@"com.Oujia.Dance"]){
  149. gameType = @"Dance";
  150. }
  151. AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:nil];
  152. [sessionManager POST:@"http://172.16.14.127:8080/upload" parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData){
  153. [formData appendPartWithFileData:upData name:@"file" fileName:[NSString stringWithFormat:@"ios_%@_%@.txt",gameType,[self getNowTimeTimestamp]] mimeType:@"text/plain"];
  154. }progress:^(NSProgress * _Nonnull uploadProgress){
  155. NSLog(@"测试数据 上传进度 = %@",uploadProgress);
  156. }success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject){
  157. NSLog(@"测试数据 上传成功");
  158. }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
  159. for (NSString * key in error.userInfo){
  160. if ([key isEqualToString:@"com.alamofire.serialization.response.error.data"]){
  161. id errorObject = [NSJSONSerialization JSONObjectWithData:error.userInfo[key] options:1 error:nil];
  162. NSLog(@"测试数据 上传失败 ===>> %@",errorObject);
  163. if (errorObject !=nil && [errorObject isKindOfClass:[NSDictionary class]]){
  164. NSString * result = errorObject[@"result"];
  165. if ([result intValue] ==1){
  166. [PopupView showCusHUDA:@"测试数据 上传成功"];
  167. }
  168. }
  169. }
  170. }
  171. }];
  172. }
  173. //获取当前时间戳有两种方法(以秒为单位)
  174. -(NSString *)getNowTimeTimestamp{
  175. NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
  176. [formatter setDateStyle:NSDateFormatterMediumStyle];
  177. [formatter setTimeStyle:NSDateFormatterShortStyle];
  178. [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  179. //设置时区,这个对于时间的处理有时很重要
  180. NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
  181. [formatter setTimeZone:timeZone];
  182. NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
  183. NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
  184. return timeSp;
  185. }
  186. @end