AFNetworkReachabilityManager says no network
Asked Answered
H

3

7

I'm confused due to lack of examples, so I did this in my appDelegate's didFinishLaunching:

[[AFNetworkReachabilityManager sharedManager] startMonitoring];
bool isThere = [[AFNetworkReachabilityManager sharedManager] isReachable];

And that always returns false, in spite of the network being there and working.

Two questions:

1) if I'm not looking for changes in status, do I need startMonitoring?

2) is there anything you need to do before reading isReachable? Do you need to wait?

Hoad answered 16/4, 2014 at 17:27 Comment(0)
C
17

I know it's too late to answer. If anybody looking for this.

Determining network status requires little time. So, calling isReachable right after startMonitoring will always return false.

You can call isReachable inside setReachabilityStatusChangeBlock ;

    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
          NSLog(@"status changed");
         //check for isReachable here
    }];
Calciferous answered 8/9, 2014 at 11:49 Comment(2)
Excellent solution, not too late at all!Hoad
Nice solution ,Thank'sFlocky
M
3

Hey I am very late to post the answer, but when I saw this question I remembered how long I've spent time to get one working code to check if network is available and finally found this code. Connectivity variable is a Bool which will give momentary network status when accessed, use of this code block in a class will give you real time network status. Its in swift hope someone will find it useful. Thanks

func checkNetworkStatus(completion:@escaping (_ connected:Bool) ->())
{
    let reachability = AFNetworkReachabilityManager.shared()
    reachability.startMonitoring();
    reachability.setReachabilityStatusChange({ (status) -> Void in

        switch(status) {

        case .unknown:
            self.connectivity = false
            completion(false)

        case .notReachable:
            self.connectivity = false
            completion(false)

        case .reachableViaWWAN:
            self.connectivity = true
            completion(true)

        case .reachableViaWiFi:
            self.connectivity = true
            completion(true)
        }
    })
}

This code block can be used in your class as

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.initNetworkMonitoring { (status) in
        // make necessary UI updates
    }
Marplot answered 24/4, 2018 at 6:51 Comment(0)
M
0

isReachable is done by Apple API SCNetworkReachabilityGetFlags. This is a block call, so AFN call it in a background queue:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
    SCNetworkReachabilityFlags flags;
    if (SCNetworkReachabilityGetFlags(self.networkReachability, &flags)) {
        AFPostReachabilityStatusChange(flags, callback);
    }
});

If you try https://github.com/tonymillion/Reachability you get the result immediately then. But of course it may cause the problem like this, main thread blocked on SCNetworkReachabilityGetFlags

-(BOOL)isReachable
{
    SCNetworkReachabilityFlags flags;  

    if(!SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
        return NO;

    return [self isReachableWithFlags:flags];
}
Mcdaniels answered 21/2, 2017 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.