iosafnetworkingafnetworking-2reachabilitycaptivenetwork

AFNetworking reachability manager for domain - always reachable despite captive portal


I'm using AFNetworking's reachability manager to monitor reachability for a specific domain, as illustrated below:

reachabilityManager = [AFNetworkReachabilityManager managerForDomain:@"www.xyz.com"];
[reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusReachableViaWWAN:
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    DLog(@"***became reachable***");
                    break;
                case AFNetworkReachabilityStatusNotReachable:
                default:
                    DLog(@"***became UNreachable***");
                    break;
            }
        }];
[reachabilityManager startMonitoring];

It works well except when the device is connected to a captive portal/network with all other data disabled. For some reason the manager believes that the device is in a reachable state, despite the fact that the domain is in fact unreachable (if one were to input the corresponding URL in a browser, it redirects to the captive portal's sign-in page).

Am I doing something wrong? If not, is there any way around this?

Thanks!


Solution

  • Unfortunately reachability just checks to see if a particular host or dns name responds. Captive portals work, as you have seen from the browser, by responding to all requests in order to display the sign-in page no matter what site the user attempts to access. So reachability says that the domain is reachable because it got a response. It can't tell the difference between the "real" response and the captive portal.

    You will need to make your code attempt to retrieve a known piece of information from your site when it is "reachable" - something like "http://example.com/signature.txt" where signature.txt contains the text you can check for. If you get the appropriate value back then you know you are really talking to your site. If not then it is probably a captive portal