| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- //
- // NetworkingMonitor.m
- // SeeTheDay
- //
- // Created by qiuhui on 2021/8/12.
- //
- #import "NetworkingMonitor.h"
- #import "AFNetworking.h"
- @interface NetworkingMonitor()
- @property (nonatomic, strong) NSString *title;
- @property (nonatomic, strong) NSString *message;
- @property (nonatomic, strong) NSString *cancelTitle;
- @property (nonatomic, strong) NSString *confirmTitle;
- @end
- @implementation NetworkingMonitor
- - (void)listenNetWorkingPort {
-
- AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
-
- [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
-
- /*
- AFNetworkReachabilityStatusUnknown = -1,
- AFNetworkReachabilityStatusNotReachable = 0,
- AFNetworkReachabilityStatusReachableViaWWAN = 1,
- AFNetworkReachabilityStatusReachableViaWiFi = 2,
- */
- switch (status) {
- case AFNetworkReachabilityStatusUnknown: // 未知网络
- NSLog(@"未知网络");
- // self.connectStatus = kNetwork_Not_Reachable;
- // [self showAlertForStatus:self.connectStatus];
- break;
- case AFNetworkReachabilityStatusNotReachable: // 没有网络
- NSLog(@"没有网络");
- self.connectStatus = kNetwork_Not_Reachable;
- [self showAlertForStatus:self.connectStatus];
- [[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"0" userInfo:nil];
- //
- break;
- case AFNetworkReachabilityStatusReachableViaWWAN: // 移动数据
- NSLog(@"移动数据");
- [[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"1" userInfo:nil];
- // [self checkConnectForStatus:status];
- break;
- case AFNetworkReachabilityStatusReachableViaWiFi: // WiFi网络
- NSLog(@"WiFi网络");
- [[NSNotificationCenter defaultCenter] postNotificationName:@"monitorNetworking" object:@"2" userInfo:nil];
- // [self checkConnectForStatus:status];
- break;
- default:
- break;
- }
- // if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
- // NSLog(@"有网");
- // }else{
- // NSLog(@"没网");
- // }
- }];
-
- [manager startMonitoring];
- }
- -(void)internetStatus
- {
- AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
- [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
- if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
- NSLog(@"有网");
- // [[NSNotificationCenter defaultCenter] postNotificationName:@"checkNetworking" object:@"yes" userInfo:nil];
- }else{
- NSLog(@"没网");
- // [[NSNotificationCenter defaultCenter] postNotificationName:@"checkNetworking" object:@"no" userInfo:nil];
- }
- }];
-
- [manager startMonitoring];
- }
- - (void)showAlertForStatus:(NetworkStatus)status {
-
- [self prepareContentForStatus:status];
-
- if (status == kNetwork_Works) {
- return;
- }
-
- /*
- BOOL isFirst = [UserDefaults boolForKey:ISFIRST_NOINTERNET];
- if (!isFirst) {
- [UserDefaults setBool:YES forKey:ISFIRST_NOINTERNET];
- [UserDefaults synchronize];
- LSLog(@"第一次登录");
- NoInternetCtrl * nointerfaceVC = [[NoInternetCtrl alloc]init];
- // UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
- // [window addSubview:nointerfaceVC.view];
-
- UIViewController * currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
- nointerfaceVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
- [currentVC presentViewController:nointerfaceVC animated:YES completion:nil];
- // UIWindow * window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
- // [window addSubview:nointerfaceVC.view];
- // UIViewController * currentVC = [window rootViewController];
-
- }else{
- LSLog(@"不是第一次登录");
- OtherNoInternetCtrl * otherNoInternetCV = [[OtherNoInternetCtrl alloc]init];
- UIViewController * currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
- otherNoInternetCV.modalPresentationStyle = UIModalPresentationOverFullScreen;
- [currentVC presentViewController:otherNoInternetCV animated:YES completion:nil];
-
- // NSDictionary * dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"homepage",@"nointernet", nil];
- // NSNotification * notification = [NSNotification notificationWithName:@"HOME_PAGENOINTERNET" object:nil userInfo:dict];
- // [[NSNotificationCenter defaultCenter] postNotification:notification];
- }
-
-
-
-
-
- /*
- // UIViewController *currentVC = [[UIViewController alloc]init];
- // UIWindow * window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
- // [window addSubview:currentVC];
- UIViewController * currentVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
-
- if(IOS8){//如果是iOS8及以上
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:self.title message:self.message preferredStyle:UIAlertControllerStyleAlert];
-
- if (self.cancelTitle && ![self.cancelTitle isEqualToString:@""]) {
- UIAlertAction *first = [UIAlertAction actionWithTitle:self.cancelTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- // NSURL *url = [NSURL URLWithString:@"App-Prefs:root=Safari"];
- // if ([[UIApplication sharedApplication] canOpenURL:url]) {
- // [[UIApplication sharedApplication] openURL:url];
- // }
- }];
- [alert addAction:first];
- }
-
- if (self.confirmTitle && ![self.confirmTitle isEqualToString:@""]) {
- UIAlertAction *second = [UIAlertAction actionWithTitle:self.confirmTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- if ([[UIApplication sharedApplication] canOpenURL:url]) {
- [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
- }
- }];
- [alert addAction:second];
- }
-
- [currentVC presentViewController:alert animated:YES completion:^{ }];
- }else{
- // 暂时没做iOS 8 以下适配
- }
- */
- }
- - (void)prepareContentForStatus:(NetworkStatus)status {
-
- if (!_title) {
- _title = @"";
- }
- if (!_message) {
- _message = @"";
- }
- if (!_cancelTitle) {
- _cancelTitle = @"";
- }
- if (!_confirmTitle) {
- _confirmTitle = @"";
- }
-
- switch (status) {
- case kNetwork_Not_Reachable:
- _title = @"没有网络";
- _message = @"无网络连接,请打开“设置”查看是否已连接WiFi或者打开移动数据\n或点击“查看权限”确定是否已为APP打开对应权限";
- _cancelTitle = @"好的";
- _confirmTitle = @"查看权限";
- break;
- case kNetwork_Wifi_Not_Reachable:
- _title = @"无法连接到服务器";
- _message = @"\n已连接到WiFi但无法获取服务器数据\n请联系管理员";
- _cancelTitle = @"确定";
- _confirmTitle = nil;
- break;
- case kNetwork_Error:
- _title = @"网络异常";
- _message = @"无网络连接,请稍后再试";
- _cancelTitle = @"确定";
- _confirmTitle = nil;
- default:
- break;
- }
- }
- @end
|