Reputation: 1254
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!
Upvotes: 5
Views: 4742
Reputation: 115104
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
Upvotes: 2
Reputation: 4271
Go through Reachability class by Apple. It has all the functions you need. Your best bet here is to use a domain (like Google) to check for actual internet connectivity.
Summarised sample code:
Add observer in didFinishLaunchingWithOptions:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [Reachability reachabilityWithHostname:@"SOME_HOST"];
[hostReachable startNotifier];
Then, in the target method:
- (void) checkNetworkStatus:(NSNotification *)notice {
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");
break;
}
}
}
Upvotes: 0
Reputation: 40038
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");
}
Upvotes: 1