13

Is there a way to detect if wifi is enabled on an iPhone/iPad?

I am not interested to see if I can reach the Internet, for that I use the Reachability class. I just need to know if wifi has been enabled on the device.

Grateful for any tips.

Jorgen
  • 465
  • 3
  • 10
  • 24
  • 3
    By coding. What have you tried? @Jorgen – Dhanuka Apr 07 '15 at 08:41
  • 1
    These links can help you- http://stackoverflow.com/questions/12906171/iphone-wifi-on-or-off http://stackoverflow.com/questions/7975727/how-to-check-if-wifi-option-enabled-or-not – Ravi Gautam Apr 07 '15 at 08:52
  • 1
    If you are using THE Reachability class, which kind of class is it? Is it the Reachability project from github? Because than it is also possible to detect which connection type is available. Also provide us with more code so we can see what you already have tried? – Alex Cio Apr 07 '15 at 08:55
  • I've actually just been researching and have come up with nothing that can detect if the radio is switched on. – Jorgen Apr 07 '15 at 14:48
  • @ Ravi Gautam absolutely brilliant. Weird but works a charm. Thank you! – Jorgen Apr 07 '15 at 15:05

3 Answers3

7

Maybe this is what you are looking for:

DEAD LINK: http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick

Wayback Machine Archive: https://web.archive.org/web/20161114213529/http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick

There isn't a framework to what you want to do, but there is a trick that might work. If you list the available interfaces, there will be some interfaces that just appear when the wifi is turned on (and some just appear when you are connected to one. You can list the interfaces like this:

struct ifaddrs *interfaces;
 
if(!getifaddrs(&interfaces)) {
    for( struct ifaddrs *interface = interfaces; interface; interface=interface->ifa_next) {
        BOOL up = (interface->ifa_flags & IFF_UP) == IFF_UP;
        if ( up ) {
            NSLog(
                @"Name : %s, sa_family : %d",
                interface->ifa_name,
                interface->ifa_addr->sa_family
            );
        }
    }
}

Output with Wifi off:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : awdl0, sa_family : 18

Output with wifi on:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : awdl0, sa_family : 18
Name : awdl0, sa_family : 30

Output with wifi on and connected:

Name : lo0, sa_family : 18
Name : lo0, sa_family : 30
Name : lo0, sa_family : 2
Name : lo0, sa_family : 30
Name : pdp_ip0, sa_family : 18
Name : pdp_ip0, sa_family : 2
Name : en0, sa_family : 18
Name : en0, sa_family : 30
Name : en0, sa_family : 2
Name : awdl0, sa_family : 18
Name : awdl0, sa_family : 30

If you explore the ifaddrs structure you will find also the BSSID/SSID of the connected network.

Community
  • 1
  • 1
gbuzogany
  • 1,911
  • 16
  • 17
  • I recommend reading the comments on that link, especially the ones talking about support. According to those, this does not work on iPhone 4s and Gen 1 iPad. I think that's because those devices do not support Wi-Fi Direct, and as such have no such interface – André Fratelli Dec 22 '15 at 04:33
  • Seems to work, but tested on 1 device only----> if (awdl0Name > 1 && en0Name == 1) { print("Wifi is on and not connected") } else if (awdl0Name > 1 && en0Name > 1) { print("Wifi is on and connected") } – Sahil Khanna Sep 20 '18 at 09:54
-1

There are several questions referring the same topic. Did you try this code from Apple?

@property (retain, nonatomic)  Reachability* reach;

self.reach = [Reachability reachabilityForInternetConnection]; //retain reach
[self.reach startNotifier];

NetworkStatus remoteHostStatus = [self.reach currentReachabilityStatus];

if(remoteHostStatus == NotReachable) {NSLog(@"init **** Not Reachable ****");}
else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"int **** wifi ****"); }
else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"init **** cell ****"); }
Pablo A.
  • 2,042
  • 1
  • 17
  • 27
-2

There is a project on github.. Imported inside your project, you can check connectivity like this:

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

NetworkStatus status = [reachability currentReachabilityStatus];

if(status == NotReachable) 
{
    //No internet
}
else if (status == ReachableViaWiFi)
{
    //WiFi
}
else if (status == ReachableViaWWAN) 
{
    //3G
}

The code from here.

Community
  • 1
  • 1
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • 3
    Thanks, but I'm not looking to see if it's connected to the Internet, just if wifi is enabled. – Jorgen Apr 07 '15 at 14:47
  • Than you will need to check @gbuzogany s link! Its more complicated but should be the way to go. – Alex Cio Apr 07 '15 at 14:51