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!
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
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With