Determine whether iPhone is really connected to th

2019-02-13 15:35发布

问题:

I'm having quite a lot of trouble on identifying rigorously the kind of network access an iPhone has. I have seen many questions like this one on StackOverflow but none of them helped me. For example I have already seen this: How to check for an active Internet connection on iOS or OSX?

But I want to know precisely when the 3 following cases occur:

  1. Wifi and 3G are disabled

  2. Wifi or 3G are enabled but there is no internet connection. It could be due to 3G not really working, even though it is showing up on the status bar, or the WiFi is a hotspot asking to log in before letting you access the Internet or last scenario: The WiFi source is a local network but does not provide any Internet connection.

  3. Everything works fine

Of course I have tried various stuff, like this for instance:

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
    /* NOT REACHABLE */
    dispatch_async(dispatch_get_main_queue(), ^{
        iarcSBInternetWarning.visible = NO;
    });
};

reach.unreachableBlock = ^(Reachability*reach)
{
    /* REACHABLE */
    iarcSBInternetWarning.visible = YES;
};

// Start the notifier, which will cause the reachability object to retain itself
[reach startNotifier];

But this seems just to say wether Wifi or 3G are enabled and does not check for possible internet restrictions. I would be highly delighted by any answer helping me identifying the three scenarios.

Of course analyzing a direct HTTP request output from a personal server API returning a specific byte sequence would work but that is not what I would like to do. I'd prefer using any kind of Reachability API.

I've used Apple's reachability API, it does not seem to recognize hotspot redirections.

If this is not possible with the Reachability APIs, then what would be the lowest resource-consuming method to make a simple request to a server, check that it is reachable before a specified timeout, and that the URL was not redirected to a hotspot login page (maybe only check the headers, not the whole server byte sequence output)? Is it a good idea to run such a script every 15 seconds, or would it use too much battery and/or network data?

回答1:

The simplest method to check outbound connectivity is to GET Google's generate204 page and check the response code

http://www.google.com/generate_204

Or you can use

http://www.apple.com/library/test/success.html which is the page the OS uses