0

I need to get the device IP of the WiFi interface.

According to several StackOverflow threads, we could assume that "en0" corresponds to the Wi-Fi interface name : https://stackoverflow.com/a/30754194/12866797

However, this feels like some kind of convention, not a standard.

Is there any consistent/standard way to retrieve the WiFi interface or the device WiFi IP address, using the iOS SDK ?

It would be nice if the API is available starting from iOS 11 but I won't be picky.

My best attempt was to use NWPathMonitor (iOS 12+) and monitor network changes corresponding to WiFi interfaces (NWInterface.InterfaceType.wifi) :

- (void) MonitorWifiInterface
{
    m_pathMonitor = nw_path_monitor_create_with_type(nw_interface_type_wifi);
    
    nw_path_monitor_set_update_handler(m_pathMonitor, ^(nw_path_t  _Nonnull path) {
        NSLog(@"[NetInterfaceUtilies] Network path changed");
            
        nw_path_enumerate_interfaces(path, ^ bool (nw_interface_t _Nonnull itf)
        {
            NSLog(@"[NetInterfaceUtilies] Name : %s , Index : %u", nw_interface_get_name(itf), nw_interface_get_index(itf));
            return true; // In order to continue the enumeration
        });
     });
 
     nw_path_monitor_start(m_pathMonitor); 
}

But I am not happy with it for the following reasons :

  1. NWPathMonitor is supposed to be used for monitoring network changes : I haven't managed to get network information whenever I wanted, but only when WiFi has been set on/off.
  2. I only managed to get the network interface name. But I can combine this data with the network interfaces retrieved with getifaddrs() in order to deduce the correct interface and IP : it's a step forward ?
  3. It's "only" available starting from iOS 12.
midiphony
  • 104
  • 5
  • Answered here https://stackoverflow.com/questions/30748480/swift-get-devices-wifi-ip-address – Bharat Jan 12 '21 at 19:29
  • Thanks, but I already pointed to this thread in my question. The solution relies on using the "en0" interface name, which looks like a convention and not a standard : who knows if it's going to change in future iOS versions :\ I'd like to know if there actually is an iOS API in the SDK allowing to retrieve the WiFi interface consistently. Or if the "en0" name really is an unmoving standard referencing the WiFi interface in iOS. – midiphony Jan 12 '21 at 19:59
  • Your question is absolutely correct and there is no such standard. But en0 refers to the first network interface there is, and for iPhones, there's a high possibility that it's going to be Wifi for a long time to come. – Bharat Jan 13 '21 at 19:09
  • 1
    Also, there is an API called NSHotspotHelper, but that requires a whole new permission from Apple and no-one gets it easily. – Bharat Jan 13 '21 at 19:18

0 Answers0