Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

sdk - iOS - Get device's WIFI IP Address

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

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.

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share