func isWiFiOn() -> Bool {
var address : String?
var ifaddr : UnsafeMutablePointer<ifaddrs>? = nil
guard getifaddrs(&ifaddr) == 0 else {
return true
}
defer {
freeifaddrs(ifaddr)
}
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next }
let interface = ptr!.pointee
let addrFamily = interface.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
guard let interface_name:UnsafeMutablePointer<Int8> = (interface.ifa_name) else {
continue
}
let name: String = String(cString: interface_name)
guard name == "en0" else {
continue
}
if (Int32(interface.ifa_flags) & IFF_UP) == IFF_UP {
return(true)
}
else {
return(false)
}
}
}
return (false)
}
上述代码中在部分iPhone 14 pro Max 机型上获取到的interface_name没有“en0”,暂未定位到原因
网友评论