Stefan
Stefan

Reputation: 1335

AFNetworking Reachability not recognizing internet connection for first time

I'm using AFNetworking Reachability in few views in this app. In one view, there is strange problem. In every view, I've started monitoring in loadView: [[AFNetworkReachabilityManager sharedManager] startMonitoring], and calling isReachable in viewDidLoad: if([AFNetworkReachabilityManager sharedManager].isReachable).

Every time, it returns NO, like there is no internet connection, and I'm pretty sure I have stable wireless connections(working in another views).

Upvotes: 3

Views: 2091

Answers (2)

Seliver
Seliver

Reputation: 353

[AFNetworkReachabilityManager sharedManager] is a singleton and it is initialized when you make call to it for the first time, and it will live while the app live.

Also it is need some time to check if the ethernet/wifi isReachable.

So you can put this code :

[[AFNetworkReachabilityManager sharedManager] startMonitoring];

to AppDelegate and just check the ethernet connection whenever you need.

Upvotes: 2

Surya Subenthiran
Surya Subenthiran

Reputation: 2217

In AFNetworking framework the startMonitoring method runs in Background global queue.

In your case ViewDidLoad method gets called before the completion of startMonitoring method because it is running in different queue.

So instead checking the isReachable flag use reachabilityStatusChangeBlock like following

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    switch (status) {
        case AFNetworkReachabilityStatusNotReachable:
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            break;
        default:
            NSLog(@"Unkown network status");

    }

}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];

Upvotes: 4

Related Questions