I have internet connection and can browsing with browser.
Here is my codes to check Reachability
with AFNetworking
.
- (BOOL)connected {
return [AFNetworkReachabilityManager sharedManager].reachable;
}
And In ViewDidLoad
BOOL isOnline = [self connected];
if(isOnline == YES)
{
NSLog(@"YES");
}
else
{
NSLog(@"NO");
}
It's only showing NO
and i don't know why is it?
Is there easiest way to check Reachability
with AFNetworking
?
I guess startMonitoring
isn't called, try to do the below:
- (void)viewDidLoad {
[super viewDidLoad];
....
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
If above answer is not solving your issue,
then your problem might be due to calling [AFNetworkReachabilityManager sharedManager].reachable
while it is in the middle of 'startMonitoring
' process where it would always return NO
.
I had the same issue. I was calling web service while AFNetworkReachabilityManager
had not finished monitoring process and was returning reachable = NO
although I had working internet connection.
- (void) callWebService {
NSLog(@"Calling Webservice...");
if ([AFNetworkReachabilityManager sharedManager].reachable == NO) {
NSLog(@"%@", kErrorNoInternet);
return;
}
// Now, proceed to call webservice....
}
So, to solve this I did a trick. Called web service after some delay (in this example 0.05 sec).
Before:
[self callWebService];
Output:
After:
[self performSelector:@selector(callWebService) withObject:nil afterDelay:0.3]; // you can set delay value as per your choice
Output:
You can see from the output, the time difference is hardly 0.05 sec (exact value 0.048 sec).
Hope this will help.
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