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 always return that there is a valid internet connection, even though the internet is turned off. This is my code:
-(BOOL)connected {
__block BOOL reachable;
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"No Internet Connection");
reachable = NO;
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WIFI");
reachable = YES;
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G");
reachable = YES;
break;
default:
NSLog(@"Unkown network status");
reachable = NO;
break;
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
}];
return reachable;
}
This method is called from my viewDidLoad method. Is there something wrong with my code, since it isn't working?
That's because that block is only executed when reachability changes.
To get the current status, you can do this:
As per my experience,
takes a moment to execute
setReachabilityStatusChangeBlock
so there is no use returning value from-(BOOL)connected
.So you can perform some task once network is connected/disconnected.
The following function is best to check connectivity at the spot (synchronously):
You have two code errors that will prevent your code from working.
First, your call to 'start monitoring' occurs inside the block that is fired in response to reachability changes -- therefore, the block will never fire (unless you start monitoring elsewhere).
Second, there are no guarantees on when that block will fire. As a result, you'll always see 'reachable' return whatever value it was initialized to -- apparently this is usually null. Even if you fix the first error, there's no guarantee (and it's actually rather unlikely) that the block will fire before you return.
Finally, on a more general level, you're also trying to test in a synchronous manner code that can only be executed asynchronously. Which is to say, it's impossible for the reachability manager to accurately determine reachability instantaneously. It can say 'I think I'm on WiFi' or 'I think I'm on 3G', but it also needs to check to see if it can actually reach the internet (hence the name reachability) before it can be certain. Reachability isn't just about 'I have a wifi' or 'I have a 3g' connection, it's also about whether you can reach the internet through it. This means the device actually has to send a request, and while this can, technically, be done in a synchronous manner, every single rule in the book insists that network code must be done asynchronously, and for good reason. If you did is synchronously, you'd block your UI until the network call returns... possibly a good 30, 40, 50, even 60 seconds later, depending on the relevant timeouts.
What you should do is start the reachability singleton monitoring in your app did load method of the app delegate, then retrieve it's data later. Without knowing the structure of your app I won't even try to guarantee accuracy, but that should at least help, somewhat.
Try this,
In AppDelegate, didFinishLaunchingWithOptions method
In HttpManager class,
In yourViewController,
Note: Don't check networkRechability at very first. Because it will not work. So please wait minimum 1 sec to check it.
You're making this more difficult than it needs to be. Try this:
If you also want to be notified when the status changes, then implement
setReachabilityStatusChangeBlock
Hope this helps!