Based on: http://www.enigmaticape.com/blog/determine-wifi-enabled-ios-one-weird-trick
Wifi status can be determined to be ON/ OFF using C based ifaddress struct from:
ifaddrs.h, and
net/if.h
[Code source: unknown.]
- (BOOL) isWiFiEnabled {
NSCountedSet * cset = [NSCountedSet new];
struct ifaddrs *interfaces;
if( ! getifaddrs(&interfaces) ) {
for( struct ifaddrs *interface = interfaces; interface; interface = interface->ifa_next) {
if ( (interface->ifa_flags & IFF_UP) == IFF_UP ) {
[cset addObject:[NSString stringWithUTF8String:interface->ifa_name]];
}
}
}
return [cset countForObject:@"awdl0"] > 1 ? YES : NO;
}
Swift 3 version (requires bridging header with #include <ifaddrs.h>
):
func isWifiEnabled() -> Bool {
var addresses = [String]()
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return false }
guard let firstAddr = ifaddr else { return false }
for ptr in sequence(first: firstAddr, next: { $0.pointee.ifa_next }) {
addresses.append(String(cString: ptr.pointee.ifa_name))
}
freeifaddrs(ifaddr)
return addresses.contains("awdl0")
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…