iOS Reachability fails to catch the case where connected to WiFi but not logged in

霸气de小男生 提交于 2019-12-01 09:25:33
...
[Reachability reachabilityWithHostname: @"http://www.google.com"]
...

You should be using the hostname (without http://) per Apple's documentation.

You are using Reachability wrong. Apple's documentation is very bad so ignore it.

The trick is to attempt to make a network connection without consulting Reachability first. NSURLConnection will fire up the radios and make the connection as needed. Be sure to handle errors, of course.

Reachability is still useful to signal when the network comes back online after going offline. You can retry to connect at that point if you have information that you weren't able to send/receive beforehand.

Also, you shouldn't call Reachability on the main thread. On poor quality networks, especially with high packet loss or broken DNS, Reachability will hang your app for more than 20 seconds and the system will kill you.

I use reachability to proof the network layer. When it should be ok than I try to load a one byte file from my server to be really sure. Performance is ok.

BOOL isOnline =  [[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined] != YES || [[RKClient sharedClient] isNetworkReachable];

    if(isOnline){
        NSData *data = [NSData dataWithContentsOfURL:[[URLManager sharedInstance]availabilityCheckURL]];

        NSString *strTemp = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        isOnline = [strTemp isEqualToString:@"1"];
    }

The Reachability example uses SystemConfiguration's Reachability API:

A remote host is considered reachable when a data packet, sent by an application into the network stack, can leave the local device. Reachability does not guarantee that the data packet will actually be received by the host.

It probably does two things:

  • (Repeatedly?) Resolve the hostname to an IP address.
  • Watch the routing table and deliver a callback when the "reachability" of that IP changes.

In the case of a captive portal (a.k.a. Wi-Fi login page), http://www.example.com has to appear to your web browser as a site that redirects you to the login page; this means www.example.com has to resolve (whether to its actual IP or not) and there has to be a site at that IP to redirect you to the login page.

Reachability is mainly useful for detecting things like airplane mode or getting disconnected from Wi-Fi.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!