Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

ios - iPhone Wifi on or off?

Within iOS framework, how can one check if the Wifi radio is enabled by the user or not? Please note that I'm not interested in the reachability through Wifi but rather if the device is turned off by the user. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...