I could find lots of guides or sample codes about checking WiFi SSID or other informations, but none about WiFi power status, eg. know if WiFi is turned on or off.

This can be done and it’s pretty simple: when it’s on, the awdl0 interface is enabled, and it’s disabled when it’s off.

You can use this simple function to check that.

func isWiFiOn() -> Bool {
        var address : String?
        var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
        if getifaddrs(&ifaddr) == 0 {
            var ptr = ifaddr
            while ptr != nil {
                defer { ptr = ptr.memory.ifa_next }
                let interface = ptr.memory
                let addrFamily = interface.ifa_addr.memory.sa_family
                if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
                    if let name = String.fromCString(interface.ifa_name) where name == "awdl0" {
                        if((Int32(interface.ifa_flags) & IFF_UP) == IFF_UP) {
                            return(true)
                        }
                        else {
                            return(false)
                        }
                    }
                }
            }
            freeifaddrs(ifaddr)
        }
        return (false)
    }

 

Vuoi saperne di più?
Lasciami la tua email. Ti scriverò i segreti su come monetizzare con il Self Publishing.

Non basta pubblicare, e se vuoi fare soldi non hai alternative.
I tuoi dati saranno utilizzati per comunicazioni informative e commerciali. Nessun dato sarà condiviso con terze parti.