AFNetworking 2.0 Reachability

后端 未结 7 1389
误落风尘
误落风尘 2020-12-07 15:33

I am trying to determine if the user is connected to the internet by using AFNetworking 2.0 and the \"AFNetworkReachabilityManager\", but it doesen\'t seem to work. It\'s al

7条回答
  •  天命终不由人
    2020-12-07 16:30

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
    
        [self checkNetworkReachability];
    }    
    
    
    +(BOOL) isReachable{
       return [AFNetworkReachabilityManager sharedManager].reachable;
    }
    
    +(void) checkNetworkReachability{
            [[AFNetworkReachabilityManager sharedManager] startMonitoring];
            [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
                NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
    
                // Check the reachability status and show an alert if the internet connection is not available
                switch (status) {
                    case -1:
                        // AFNetworkReachabilityStatusUnknown = -1,
                        NSLog(@"The reachability status is Unknown");
                        break;
                    case 0:
                        // AFNetworkReachabilityStatusNotReachable = 0
                        NSLog(@"The reachability status is not reachable");
                        break;
                    case 1:
                        NSLog(@"The reachability status is reachable via wan");
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"Network_Reachable" object:nil];
                        break;
                    case 2:
                        // AFNetworkReachabilityStatusReachableViaWiFi = 2
                        NSLog(@"The reachability status is reachable via WiFi");
    
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"Network_Reachable" object:nil];
                        break;
    
                    default:
                        break;
                }
    
            }];
        }
    

提交回复
热议问题