Refund FANSE to achieve AFNetworking compliance?

I have an internet connection and browsing in a browser.

Here are my codes to check Reachability on AFNetworking .

 - (BOOL)connected { return [AFNetworkReachabilityManager sharedManager].reachable; } 

And in ViewDidLoad

 BOOL isOnline = [self connected]; if(isOnline == YES) { NSLog(@"YES"); } else { NSLog(@"NO"); } 

It shows only NO , and I do not know why this is?

The easiest way to test Reachability on AFNetworking ?

+6
source share
3 answers

I think startMonitoring not being called, try the following:

 - (void)viewDidLoad { [super viewDidLoad]; .... [[AFNetworkReachabilityManager sharedManager] startMonitoring]; } 
+17
source

If the answer above does not solve your problem, then your problem may be caused by calling [AFNetworkReachabilityManager sharedManager].reachable while it is in the middle of the startMonitoring process, where it will always return NO .

I had the same problem. I called the web service, but AFNetworkReachabilityManager did not finish the monitoring process and returned reachable = NO , although I had an 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 the trick. The called web service after some delay (in this example, 0.05 seconds).

Before:

 [self callWebService]; 

Output: enter image description here

After:

 [self performSelector:@selector(callWebService) withObject:nil afterDelay:0.3]; // you can set delay value as per your choice 

Output:

enter image description here

You can see at the output, the time difference is hardly 0.05 s (the exact value is 0.048 seconds).

Hope this helps.

+1
source

instead of waiting, you can use blocks to make sure that your web service will only be called when there is a network.

 [[AFNetworkReachabilityManager sharedManager]startMonitoring]; [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) { // connected. you can call your web service here }else { // internet disconnected } }]; 
0
source

All Articles