How to resolve mDNS address on IOS?

Hi all,


I am pretty new to IOS. Can anyone point me in the right direction on how to resolve mDNS addresses on IOS?

I have a network with multiple devices named : "http://mydevice.local", and ultimately I want an array with the IP-addresses of these devices. Any help is appriciated a lot 🙂


Thanks.

Do you actually need to resolve the name to IP addresses? In most cases you can pass the name to a connect-by-name API and it will resolve internally. That’s what I recommend in general, partly because it’s less code and partly because it handles various non-obvious edge cases.

If you really do need to resolve the address (you’re working with UDP, say), you can call any DNS resolving routine and it will handle

foo.local.
names correctly. The two most common choices are:
  • CFHost, for async resolution

  • getaddrinfo
    , for UNIX-y code

Make sure you handle both IPv4 and IPv6 results; IPv6 results are especially common when dealing with local names.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for your answer. I do have to resolve the IP - I have multiple embedded devices running small webservers (think Chromecast / apple TV), that the app should be able to communicate with. But will your solution also work for multiple devices with the same DNS-address ("mydevice.local") ?.. :-)

Thank you for your answer. I do have to resolve the IP - I have multiple embedded devices running small webservers (think Chromecast / apple TV), that the app should be able to communicate with. But will your solution also work for multiple devices with the same DNS-address ("mydevice.local") ?.. :-)

I’m confused by this response. Each of these devices must necessarily have a unique host name (

foo.local.
). If you want to talk to the device using HTTP[S] APIs, you don’t need to resolve IP addresses yourself. Just take that host name and embed it in a URL (
http://foo.local.
) and the underlying infrastructure will take care of name-to-address resolution.

Two points:

  • You can get this host name from NSNetService by resolving the service and then accessing the

    hostName
    property.
  • The best way to build URLs from parts is NSURLComponents.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi I have questions to this post.


I have an ESP8266 as a Webserver in my local network. I can connect via Browser with "esp8266.local"


How Can I do an http request to this in Swift?


Do I first get the IP address by this mdns and then do an http request?

How to resolve mDNS address on IOS?
 
 
Q