Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking detect when wifi connection without active internet connection

I'm using AFNetworking and was wondering how to detect a scenario when user is connected to a wifi network without active internet connection.

I reproduce this scenario buy powering a router without connecting the dsl line.

AFNetworking return  AFNetworkReachabilityStatusReachableViaWiFi = 2

my code:

 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
        self.isInternetAvialable = status > 0;
    }];

thanks

I refacotred the code to be like

  AFNetworkReachabilityManager* manager = [AFNetworkReachabilityManager managerForDomain:@"http://www.google.com"];
        [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
            self.isInternetAvialable = status > 0;
        }];
        [manager startMonitoring];

now the block never get called!

like image 634
ibm123 Avatar asked Oct 21 '22 08:10

ibm123


2 Answers

Rather than using [AFNetworkReachabilityManager sharedManager] which just monitors network connectivity, you can use [AFNetworkReachabilityManager managerForDomain:(NSString *)domain] and pass an appropriate hostname - such as "www.google.com" - this way you can test whether the network you are connected to has Internet access - it can still be fooled by local DNS and a web server, but I would think that it is unlikely someone would do that.

Once you have your AFNetworkReachabilityManager you can set the change block as you do for the shared manager

like image 99
Paulw11 Avatar answered Oct 29 '22 19:10

Paulw11


Use Reachability class and its reachabilityForInternetConnection method.

Reachability *networkReachability = [Reachability reachabilityForInternetConnection];   
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];    

if (networkStatus == NotReachable) 
{        
    NSLog(@"There is NO Internet connection");        
} 
else 
{        
     NSLog(@"There is Internet connection");        
}
like image 40
Rafa de King Avatar answered Oct 29 '22 18:10

Rafa de King