1

I want to be able to check to see if the user is connected to WiFi, but not connected to a network. So basically I want to check the state of the WiFi button on the device setting page to check if button is enabled or disabled.

At the moment I can check to see if the Wifi is connected to a network or not connected to an network doing the following:

BOOL hasWiFiNetwork = NO;
NSArray *interfaces = CFBridgingRelease(CNCopySupportedInterfaces());
for (NSString *interface in interfaces)
{
    NSDictionary *networkInfo = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interface)));
    if (networkInfo != NULL)
    {
        hasWiFiNetwork = YES;
        break;
    }
    else
    {
        hasWiFiNetwork = NO;
        break;
    }
}
YaBCK
  • 2,949
  • 4
  • 32
  • 61
  • https://github.com/tonymillion/Reachability –  Jun 25 '15 at 12:07
  • @Dan Reachability just does the same as my code above, I actually want to check the state of the Wifi button on setting page – YaBCK Jun 25 '15 at 12:11
  • well from the answer below, I would like to disagree with you on that. –  Jun 25 '15 at 12:12
  • @Dan the answer below doesn't give me what I need, read my question again please. – YaBCK Jun 25 '15 at 12:15
  • Check this out: http://stackoverflow.com/a/25956280/1587449 but i don't know if it is still up to date. – Da Maex Jun 25 '15 at 12:29
  • have you checked http://stackoverflow.com/a/7975750/3633534 – Sujay Jun 25 '15 at 12:30
  • @Sujay I'm not trying to find if the user is connected to a network, or isn't connected. I'm just trying to find the state of the actual WiFi on iPad. So if WiFi button is enabled then message something, if it's disabled then message something. – YaBCK Jun 25 '15 at 12:33
  • It turns out its not possible: http://stackoverflow.com/a/12906461/4657588 –  Jun 25 '15 at 12:46

3 Answers3

2

Try this code and use Reachability class.

BOOL isConnectedProperly = ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWiFi);
Om Prakash
  • 9,191
  • 1
  • 12
  • 20
  • I like this answer, Reachability is great for this type of task. –  Jun 25 '15 at 12:13
  • This answer only gives a value of 1 or 0 (True or False) and this is if the person is connected to a WiFi network. – YaBCK Jun 25 '15 at 12:14
1

Update

It turns out that it isn't possible, there is no API/Framework/BOOL value that can do this because Apple havn't added any kind of ability to check to see if the WiFi is switched on or off for developers. As explained nicely here: https://stackoverflow.com/a/12906461/4657588


Then this SO post should be what you want: https://stackoverflow.com/a/7938778/4657588

Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus status = [reachability currentReachabilityStatus];

if (status == ReachableViaWiFi) {
    // WiFi
}

else {
    // Either WiFi is off or we are not connected to a WiFi network.
}
Community
  • 1
  • 1
  • I know this gives you the status of WiFi, however if you're connected to WiFi but you're not connected to actual network. Like your home network then it doesn't give you anything back. Because it only checks if you're connected to a network. – YaBCK Jun 25 '15 at 12:22
  • @ChrisBeckett Well if it gives nothing back then can't you assume that the user either hasn't turned on WiFi or isn't connected to a network. That should be good enough for whatever alert or test it is that you want to perform. –  Jun 25 '15 at 12:27
  • Well I've tested it and the WiFi is turned on, but not connected to network and I can't display a message because I can check the status of the actually WiFi setting on iOS - I trying to perform this action for the purpose of GPS location – YaBCK Jun 25 '15 at 12:30
  • @ChrisBeckett I updated my answer to show you what I mean. The code should now more or less do what you want. –  Jun 25 '15 at 12:34
  • Dan this doesn't do what I need, becasue ReachableViaWiFi means if it's connected to a WiFi network or not. I don't want to check the state of connected/not connected to a WiFi network. I just want to check if WiFi on iPad is enabled on settings page before checking anything to do with WiFi networks. – YaBCK Jun 25 '15 at 12:37
  • If you explain in your answer what you have found out about it not being possible. I'll mark yours as the answer. – YaBCK Jun 25 '15 at 12:49
1

First you need to download reachability file from apple developer site and after that add these piece of code every time where ever yu want to check.

-(BOOL)isConnectedTointernet{
BOOL status = NO;
Reachability *reachability = [Reachability reachabilityForInternetConnection];
int networkStatus = reachability.currentReachabilityStatus;
status = (networkStatus != NotReachable)? YES:NO;
return status;}
Deepakraj Murugesan
  • 1,195
  • 11
  • 30
  • 1
    I'm not trying to find if the user is connected to a network, or isn't connected. I'm just trying to find the state of the actual WiFi on iPad. So if WiFi button is enabled then message something, if it's disabled then message something. – YaBCK Jun 25 '15 at 12:33