NetworkingMonitor.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // NetworkingMonitor.m
  3. // SeeTheDay
  4. //
  5. // Created by qiuhui on 2021/8/12.
  6. //
  7. #import "NetworkingMonitor.h"
  8. #import "AFNetworking.h"
  9. @interface NetworkingMonitor()
  10. @property (nonatomic, strong) NSString *title;
  11. @property (nonatomic, strong) NSString *message;
  12. @property (nonatomic, strong) NSString *cancelTitle;
  13. @property (nonatomic, strong) NSString *confirmTitle;
  14. @end
  15. @implementation NetworkingMonitor
  16. - (void)listenNetWorkingPort {
  17. AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
  18. [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  19. /*
  20. AFNetworkReachabilityStatusUnknown = -1,
  21. AFNetworkReachabilityStatusNotReachable = 0,
  22. AFNetworkReachabilityStatusReachableViaWWAN = 1,
  23. AFNetworkReachabilityStatusReachableViaWiFi = 2,
  24. */
  25. switch (status) {
  26. case AFNetworkReachabilityStatusUnknown: // 未知网络
  27. NSLog(@"未知网络");
  28. // self.connectStatus = kNetwork_Not_Reachable;
  29. // [self showAlertForStatus:self.connectStatus];
  30. break;
  31. case AFNetworkReachabilityStatusNotReachable: // 没有网络
  32. NSLog(@"没有网络");
  33. self.connectStatus = kNetwork_Not_Reachable;
  34. [self showAlertForStatus:self.connectStatus];
  35. [[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"0" userInfo:nil];
  36. //
  37. break;
  38. case AFNetworkReachabilityStatusReachableViaWWAN: // 移动数据
  39. NSLog(@"移动数据");
  40. [[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"1" userInfo:nil];
  41. // [self checkConnectForStatus:status];
  42. break;
  43. case AFNetworkReachabilityStatusReachableViaWiFi: // WiFi网络
  44. NSLog(@"WiFi网络");
  45. [[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"2" userInfo:nil];
  46. // [self checkConnectForStatus:status];
  47. break;
  48. default:
  49. break;
  50. }
  51. // if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
  52. // NSLog(@"有网");
  53. // }else{
  54. // NSLog(@"没网");
  55. // }
  56. }];
  57. [manager startMonitoring];
  58. }
  59. -(void)internetStatus
  60. {
  61. AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
  62. [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  63. if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
  64. NSLog(@"有网");
  65. // [[NSNotificationCenter defaultCenter] postNotificationName:@"checkNetworking" object:@"yes" userInfo:nil];
  66. }else{
  67. NSLog(@"没网");
  68. // [[NSNotificationCenter defaultCenter] postNotificationName:@"checkNetworking" object:@"no" userInfo:nil];
  69. }
  70. }];
  71. [manager startMonitoring];
  72. }
  73. - (void)showAlertForStatus:(NetworkStatus)status {
  74. [self prepareContentForStatus:status];
  75. if (status == kNetwork_Works) {
  76. return;
  77. }
  78. /*
  79. BOOL isFirst = [UserDefaults boolForKey:ISFIRST_NOINTERNET];
  80. if (!isFirst) {
  81. [UserDefaults setBool:YES forKey:ISFIRST_NOINTERNET];
  82. [UserDefaults synchronize];
  83. LSLog(@"第一次登录");
  84. NoInternetCtrl * nointerfaceVC = [[NoInternetCtrl alloc]init];
  85. // UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
  86. // [window addSubview:nointerfaceVC.view];
  87. UIViewController * currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
  88. nointerfaceVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
  89. [currentVC presentViewController:nointerfaceVC animated:YES completion:nil];
  90. // UIWindow * window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
  91. // [window addSubview:nointerfaceVC.view];
  92. // UIViewController * currentVC = [window rootViewController];
  93. }else{
  94. LSLog(@"不是第一次登录");
  95. OtherNoInternetCtrl * otherNoInternetCV = [[OtherNoInternetCtrl alloc]init];
  96. UIViewController * currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
  97. otherNoInternetCV.modalPresentationStyle = UIModalPresentationOverFullScreen;
  98. [currentVC presentViewController:otherNoInternetCV animated:YES completion:nil];
  99. // NSDictionary * dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"homepage",@"nointernet", nil];
  100. // NSNotification * notification = [NSNotification notificationWithName:@"HOME_PAGENOINTERNET" object:nil userInfo:dict];
  101. // [[NSNotificationCenter defaultCenter] postNotification:notification];
  102. }
  103. /*
  104. // UIViewController *currentVC = [[UIViewController alloc]init];
  105. // UIWindow * window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
  106. // [window addSubview:currentVC];
  107. UIViewController * currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
  108. if(IOS8){//如果是iOS8及以上
  109. UIAlertController *alert = [UIAlertController alertControllerWithTitle:self.title message:self.message preferredStyle:UIAlertControllerStyleAlert];
  110. if (self.cancelTitle && ![self.cancelTitle isEqualToString:@""]) {
  111. UIAlertAction *first = [UIAlertAction actionWithTitle:self.cancelTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  112. // NSURL *url = [NSURL URLWithString:@"App-Prefs:root=Safari"];
  113. // if ([[UIApplication sharedApplication] canOpenURL:url]) {
  114. // [[UIApplication sharedApplication] openURL:url];
  115. // }
  116. }];
  117. [alert addAction:first];
  118. }
  119. if (self.confirmTitle && ![self.confirmTitle isEqualToString:@""]) {
  120. UIAlertAction *second = [UIAlertAction actionWithTitle:self.confirmTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  121. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  122. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  123. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  124. }
  125. }];
  126. [alert addAction:second];
  127. }
  128. [currentVC presentViewController:alert animated:YES completion:^{ }];
  129. }else{
  130. // 暂时没做iOS 8 以下适配
  131. }
  132. */
  133. }
  134. - (void)prepareContentForStatus:(NetworkStatus)status {
  135. if (!_title) {
  136. _title = @"";
  137. }
  138. if (!_message) {
  139. _message = @"";
  140. }
  141. if (!_cancelTitle) {
  142. _cancelTitle = @"";
  143. }
  144. if (!_confirmTitle) {
  145. _confirmTitle = @"";
  146. }
  147. switch (status) {
  148. case kNetwork_Not_Reachable:
  149. _title = @"没有网络";
  150. _message = @"无网络连接,请打开“设置”查看是否已连接WiFi或者打开移动数据\n或点击“查看权限”确定是否已为APP打开对应权限";
  151. _cancelTitle = @"好的";
  152. _confirmTitle = @"查看权限";
  153. break;
  154. case kNetwork_Wifi_Not_Reachable:
  155. _title = @"无法连接到服务器";
  156. _message = @"\n已连接到WiFi但无法获取服务器数据\n请联系管理员";
  157. _cancelTitle = @"确定";
  158. _confirmTitle = nil;
  159. break;
  160. case kNetwork_Error:
  161. _title = @"网络异常";
  162. _message = @"无网络连接,请稍后再试";
  163. _cancelTitle = @"确定";
  164. _confirmTitle = nil;
  165. default:
  166. break;
  167. }
  168. }
  169. @end