AFNetworking Reachability Return FALSE value?
Asked Answered
F

3

6

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?

Featureless answered 23/2, 2014 at 7:23 Comment(0)
T
18

I guess startMonitoring isn't called, try to do the below:

- (void)viewDidLoad {
     [super viewDidLoad];
      ....
     [[AFNetworkReachabilityManager sharedManager] startMonitoring];   
}
Toshiatoshiko answered 23/2, 2014 at 7:47 Comment(4)
Anyway it should be there bro, but it is weird it works fine, try to register for changes, check this https://mcmap.net/q/688610/-setting-up-reachability-with-afnetworking-2-0.Toshiatoshiko
When i test with these code , it's even doesn't show NSLog bro. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status)); }];Featureless
I also added #import "AFNetworking.h" #import "AFNetworkReachabilityManager.h". What do i need?Featureless
@Yahiko do test on the real device? try to turn off wireless, switch to cellular data then back to wireless etc..Toshiatoshiko
S
1

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: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 from the output, the time difference is hardly 0.05 sec (exact value 0.048 sec).

Hope this will help.

Stacked answered 28/9, 2016 at 8:18 Comment(1)
You can also check if networkReachabilityStatus == AFNetworkReachabilityStatusUnknown. If status is Unknown then you can assume that network can be reachable and make a requestOntina
E
0

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

[[AFNetworkReachabilityManager sharedManager]startMonitoring];

[[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
{
    if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) 
{

        // connected. you can call your web service here

    }else
{
        // internet disconnected
    }
}];
Extemporize answered 12/2, 2018 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.