I am obtaining the IP address in the Wi-Fi connection of an iPhone using the following source code. Howerver, I obtained completely different results on different models (iPhone XR, iPhone 13) with the same OS (iOS16.4.1).
Do you happen to know the cause of this?
【Code】
class Wifi { enum connectionStatusItem { case unknown case onlineWifi }
class func connectionStatus() -> connectionStatusItem {
var valid = false
//Get list of all interfaces on the iPhone
var ifa : UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifa) == 0 {
var p = ifa
//For each interface
while p != nil {
defer {
p = p?.pointee.ifa_next
}
let ifp = p?.pointee
let fam = ifp?.ifa_addr.pointee.sa_family
//Check for IPv4 or IPv6 interface
if fam == UInt8(AF_INET) || fam == UInt8(AF_INET6) {
//Check Wi-Fi interface
//<-------------- 'if' statement not executing on iPhone13(iOS16.3.1) and iPhone13(iOS16.4.1)
if String(cString: (ifp?.ifa_name)!) == "en0" {
//Convert interface address to a human readable string
var addr = ifp?.ifa_addr.pointee
var host = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(&addr!,
socklen_t((addr?.sa_len)!),
&host,
socklen_t(host.count),
nil,
socklen_t(0),
NI_NUMERICHOST
)
var hosts: String?
hosts = String(cString: host)
valid = hosts != nil
}
}
}
freeifaddrs(ifa)
}
let ret : connectionStatusItem = valid ? .onlineWifi : .unknown
return ret
}
}
【Results】 success iPhone13【iOS 16.1.1】 success iPhone13【iOS 16.2.1】 failure iPhone13【iOS 16.3.1】 <------------------------- ✖ success iPhone13【iOS 16.4】 failure iPhone13【iOS 16.4.1】 <------------------------- ✖ success iPhoneXR【iOS 16.4.1】 success iPhone13【iOS 16.4.1(a)】