Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.0k views
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
        });
     });
 
     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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...