Get all available IP address of a device without using getifaddrs on iOS, iPadOS, tvOS, watchOS

Hi,

My requirement is that I want to get a list of all IP addresses associated with all network interfaces on these Apple platforms - iOS, iPadOS, tvOS, watchOS.

One of the approaches which will work for all the platforms is using getifaddrs API.

I want to know whether there exists a native approach in either Swift or Objective-C which solves this problem.

Official Apple documentation for getifaddrs API is kept in the archive section. Therefore, I am not sure whether it is a good approach to go ahead with or not. And I want to write the code in High-Level language as far as possible. So want to avoid using getifaddrs API directly as far as possible.

I went through the classes/structs such as NWInterface, NWPath, NWPathMonitor. But was not able to figure out any API which will serve my purpose.

Official Apple documentation for getifaddrs API is kept in the archive section.

Not really. What’s going on here is that:

  • getifaddrs is documented in a man page.

  • We stopped published man pages in our primary documentation (r. 16512537).

You will, for example, find that fopen is in the same boat, and no one worries about calling that (-:

So, the official docs for this stuff is the getifaddrs man page.


As to your larger question, it’s hard to say whether this is the right answer without knowing more about your problem. You wrote:

My requirement is that I want to get a list of all IP addresses associated with all network interfaces on these Apple platforms

To what end? What are you going to do with this list once you get it?

Share and Enjoy

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

Thank you for the prompt reply Eskimo.

Understood your point regarding getifaddrs.

We are essentially building a cross platform application. In one of our use cases, we want to show the list of all IP addresses to the user which would be similar to what he would get if, let's say he runs ifconfig command in macOS terminal (there is no terminal on the platforms I mentioned in the question, but I hope you get the point what we are trying to achieve here).

In one of our use cases, we want to show the list of all IP addresses to the user

OK, cool, that’s perfectly reasonable.

I see a lot of folks use getifaddrs as part of their networking code. In some cases thats the right thing to do, but in a lot of cases it’s a mistake (for example, using it to find the ‘primary’ IP address as part of an implementation of some protocol layered on top of UDP).

Share and Enjoy

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

In some cases thats the right thing to do, but in a lot of cases it’s a mistake (for example, using it to find the ‘primary’ IP address as part of an implementation of some protocol layered on top of UDP).

@eskimo In those cases, what should one use?

Get all available IP address of a device without using getifaddrs on iOS, iPadOS, tvOS, watchOS
 
 
Q