sharma.mahesh369
sharma.mahesh369

Reputation: 985

Detect whether WiFi data is enabled or not?

I need to know how to detect the WiFi state where i am getting the true return on checking isNetworkconnected() via ConnectivityManager, but actually the response is manipulated by the respected WIFI server.

Suppose I visited some place where WIFI is free but I need to login to that network for accessing, but in the mean time if I start my app without login to that wifi, I will get the WIFI connected return true via ConnectivityManager (as android only check if WIFI is connected or not), and some unexpected response from that wifi host.

How can I detect such kind of connectivity? I have seen multiple apps loader running in such case, but how they detect it?

Upvotes: 0

Views: 139

Answers (1)

Ben
Ben

Reputation: 1061

I don't think you're going to be able to test via the connectivity manager. The result will always be unreliable for the reasons you've stated.

So, as I see it, you have two options:

1) You test the Wi-Fi's SSID on like an approved list or something, and if the user isn't on that SSID, you alert them.

Here's the method to get the SSID of the currently connected router (iOS 7+):

@import SystemConfiguration.CaptiveNetwork;

/** Returns first non-empty SSID network info dictionary.
 *  @see CNCopyCurrentNetworkInfo */
- (NSDictionary *)fetchSSIDInfo
{
    NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
    NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);

    NSDictionary *SSIDInfo;
    for (NSString *interfaceName in interfaceNames) {
        SSIDInfo = CFBridgingRelease(
        CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
        NSLog(@"%s: %@ => %@", __func__, interfaceName, SSIDInfo);

        BOOL isNotEmpty = (SSIDInfo.count > 0);
        if (isNotEmpty) {
            break;
        }
    }
    return SSIDInfo;
}

2) I suggest that you forget trying to test wi-fi with these standard methods, and you simply fire a request with a timeout of like 3s before you try to use the Wi-Fi. If you get a valid response, you are connected, else it has failed.

Upvotes: 0

Related Questions