Regarding the issue of not being able to identify the Wi-Fi interface of an iPhone device

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)】

I am obtaining the IP address in the Wi-Fi connection of an iPhone using the following source code.

What are you trying to do with the IP?

Our iOS app can connect to the dashcam via wireless Wi-Fi, allowing users to make configuration changes and verify driving records please let me know why some iPhones are able to identify the Wi-Fi interface of an iPhone device?

I would recommend just leaving the IP address along and using the high level NEHotspotConfiguration and NEHotspotConfigurationManager APIs to perform the association with the dashcam network and perform any further communication from there. Getting into interface and IP juggling between devices may have lots of edge cases that there is no good was to accommodate for.

What Matt said but also…

Your current code assumes that en0 is the system’s Wi-Fi interface. That is not a valid assumption. BSD interface names are not considered API. They have changed in the past and they will change again in the future.

There are ways to get the IP addresses of a Wi-Fi interface, using code similar to what I posted yesterday, but I’m not sure that checking the IP address is the right option in this case. After all, some other Wi-Fi network could accidentally return the same IP address. What is your app going to do in that case?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Regarding the issue of not being able to identify the Wi-Fi interface of an iPhone device
 
 
Q