'Discovering services via Bonjour on iOS 14

I'm having trouble understanding privacy constraints for local network service discovery using Bonjour on iOS 14. Here is what I found:

  1. Using NetServiceBrowser, I'm able to discover services using: browser.searchForServices(ofType: "_services._dns-sd._udp", inDomain: "local.") I have to add a couple of keys to .plist file, but it works.
  2. However, if I turn to NWBrowser, and try to run the same request, I get an error:
    nw_browser_fail_on_dns_error_locked [B1] Invalid meta query type specified. 
    nw_browser_start_dns_browser_locked failed: BadParam(-65540)

I gather, this means I need the multicast entitlement.

Now, the question is, why would I go through all the trouble of requesting multicast entitlement from Apple, if I just can use NetServiceBrowser instead of NWBrowser? Doesn't NetServiceBrowser defeat the purpose of obtaining multicast entitlement?



Solution 1:[1]

According to the docs this error means that you passed an invalid value to NWBrowser - most probably it's the format of the service you're trying to discover.

You haven't posted your NWBrowser code, but for example, a wrong formatted service such as _example_.tcp would probably result in the error you're seeing. Make sure it is in the correct format, which should be something like _example._tcp..

Another thing you need to do is modify your Info.plist file and add two keys:

  • Bonjour Services - this is an array, the first item should be the service you're trying to discover, in my example, it is _example._tcp.
  • Privacy - Local Network Usage Description - that one takes an explanation text which will be shown to the user the moment you start discovery with NWBrowser.

Now regarding the entitlement you mentioned: it is only needed for apps that scan for all services on the network and not a specific one like in your case. I encourage you to watch this video, which explains everything I wrote here.

Lastly, why should you use NWBrowser over NetServiceBrowser? That's a very good question which I'm also trying to figure out the answer for ?

Solution 2:[2]

  1. The multicast entitlement is only required if you're scanning for all services. If you're scanning for a specific service - you don't need it

In iOS15 - if you do not have the entitlement you can not do

browser.searchForServices(ofType: "_services._dns-sd._udp", inDomain: "local.")
  1. The reason to use NWBrowser is that it is the new api - NetServiceBrowser is deprecated. At some point - Apple will (probably) simply remove NetServiceBrowser. It's pretty deeply ingrained in a lot of software though - so I don't see them removing it any time soon.

NWBrowser is a somewhat easier API to use as far as straightforward browsing goes.

Note though - that if you want to resolve a service to get an IP address, you'll need to use NetService anyway...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimrod Dayan
Solution 2 Confused Vorlon