Swift: Bonjour, NSNetService & IP Addresses

Swift: Bonjour, NSNetService & IP Addresses

Good morning. At least it’s a good morning here. I have recently been exploring Bonjour in Swift – I have never investigated Bonjour as a concept as networking is one of the weaker areas of my coding ability. However recently I’ve needed to look into it for work reasons.

I had been able to scan the network for a certain protocol and find devices on the network. I’d get their name but needed to resolve their IP Addresses which are not immediately available upon discovery.

I’d been using some Objective-C code and a bridging header to resolve the addresses. So here is what I was using.

Now, while this works, it’s a clutter as I wanted to keep all the code in my Swift project… Swift. Surely there had to be a way. After a quick post to StackOverflow, I was redirected to a post with some relevant code. And now I have this (directly in a┬áNSNetServiceDelegate method).

Would you look at that? A pure Swift solution. I’ve since removed my Obj-C class and bridging header and now everything is so much cleaner. And seriously – faster.

2 Replies to “Swift: Bonjour, NSNetService & IP Addresses”

  1. Thank you for this! btw Have you noticed any problems using NetServiceBrowser in the beta of XCode 8? I can’t seem to get it to discover anything.

  2. I’m glad you like it. To be honest I have not yet fired up Xcode 8. I just haven’t spent the time to install and kick the tires. I probably will later this summer – perhaps a beta update will fix things up. Have you filed a radar yet?

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.