Reputation: 475
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.
Upvotes: 13
Views: 11063
Reputation: 1929
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.
Upvotes: 7
Reputation: 2052
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 ****"); }
Upvotes: -1
Reputation: 6052
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
}
Upvotes: -2