Search code examples
iosobjective-cios8afnetworking-2reachability

AFNetworking Reachability Show UIView When Network Status Changes


I am trying to implement ReachabilityManager in a project from AFNetworking 2.

I want to slide in a UIView when the status of the network changes.

The documentation for Reachability in AFNetworking 2.0 here: http://cocoadocs.org/docsets/AFNetworking/2.4.1/Classes/AFNetworkReachabilityManager.html#//api/name/startMonitoring just points to code from Apple for their Reachability implementation example. The Apple example does not seem to implement it as needed by AFNetworkReachabilityManager in AFNetworking 2.0. I don't see an example anywhere in the documentation links provided in Git.

I have added the following to my viewWillAppear method:

[[AFNetworkReachabilityManager sharedManager] startMonitoring];

but what next? How do I check if the network status changes in my code so that I could then display a notice (in the form of a slide in UIView) to the user?


Solution

  • You have to say what it should do when the reachability status changes. For example, from the AFNetworking GitHub home page:

    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
    }];
    

    If you want to show a view, you'd do it inside this setReachabilityStatusChangeBlock.