Determine whether iPhone is really connected to the internet or just behind a restricted hotspot
Asked Answered
P

1

6

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?

Physiology answered 30/7, 2014 at 15:14 Comment(6)
Is there any specific reason you are interesting different cases? Because if your only purpose is checking your API host reachability Tony Million's version of Reachability working great.Dative
@mohacs I am willing to make a mobile substrate tweak that says wether or not you are really connected to the internet. And Tony's Million reachability library says host is reachable even if I am behind a hotspot.Physiology
Ok, I have no change to connect a hotspot right now, tested with changing connected Wi-Fi gateway address to something bogus and Reachability gave me correct result not reachable.Dative
@mohacs Thanks for your concern. I have done it also and I thought I had finally got it working but I think this is just because a server does not respond that it says not reachable. However when I keep google as an example, even though my 3G does not work or that I am behind a Wifi Hotspot it says Reachable :(Physiology
As a hotspot resolves any DNS request to the same address, the Reachability API will succeed. You have to know what is the expected content at the address you are testing to detect if you got to the right place at the end.Tyrannize
@ThomasDesert Thank you for answering, I have edited my question above. Maybe can you help me?Physiology
R
4

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

Rosenbaum answered 9/8, 2014 at 19:43 Comment(2)
Thank you for these two suggestions. The first one is very light and nifty. So for that you deserve my +1. What would then be a fast and low-consuming method to simply ask the http response code of that page without invoking too much heavy machinery API ?Physiology
I don't think you can get much lighter than NSURLConnection's sendSynchronousRequest: developer.apple.com/library/mac/documentation/cocoa/reference/…:Rosenbaum

© 2022 - 2024 — McMap. All rights reserved.