Question

I am using AFNetworking 2.2.1 and trying to use the AFNetworkReachabilityManager to check if a certain domain is reachable, but it always return NO. Maybe I am not using this correctly and would appreciate any pointers.

AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager managerForDomain:@"www.google.com"];
// always returns NO:
NSLog([manager isReachable] ? @"YES" : @"NO");

I also tried with the startMonitoring method (after the manager variable declaration), but with no luck:

[manager startMonitoring];

EDIT: As David pointed out, needed to add a status change block to get notified:

AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager managerForDomain:@"www.google.com"];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
        case AFNetworkReachabilityStatusNotReachable:
            NSLog(@"Never called");
            break;
        default:
            NSLog(@"Never called");
        break;
    }
}];
[manager startMonitoring];
// always returns NO:
NSLog([manager isReachable] ? @"YES" : @"NO");
Was it helpful?

Solution

AFReachabilityManager does it's magic asynchronously, so the results of isReachable aren't valid until it's completed. The easiest way to be notified when the result of isReachable is valid is to use setReachabilityStatusChangeBlock: to set a status changed block, and do whatever is appropriate when the status is no longer AFNetworkReachabilityStatusUnknown

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top