Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if wifi is turned on

Tags:

ios

iphone

ipad

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.

like image 884
Jorgen Avatar asked Apr 07 '15 08:04

Jorgen


People also ask

Is someone stealing my Wi-Fi?

Most routers have a series of indicator lights that let you know when the router is powered on or connected to the internet. It should also have a light that shows wireless activity. A quick way to see if you have freeloaders is to turn off all your wireless devices and see if the light is still blinking.


1 Answers

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.

like image 50
gbuzogany Avatar answered Oct 16 '22 20:10

gbuzogany