Bonjour discovery with NetServiceBrowser not working in iOS and iPadOS 14

My app has to find a certain bonjour service on the local network and my code works fine for iOS 12/13 as well as macOS Big Sur and Catalina.

The exact same code fails on iOS and iPadOS 14 with this error:
["NSNetServicesErrorDomain": 10, "NSNetServicesErrorCode": -72000]

Which I have looked into and -72000 is an "Unknown error".

I am using NetServiceBrowser to find the service and here is my code:

Code Block
class Bonjour: NSObject {
    var discovered: [DiscoveredInstance] = []
    let bonjourBrowser = NetServiceBrowser()
    var discoveredService: NetService?
    override init() {
        super.init()
        bonjourBrowser.delegate = self
        startDiscovery()
    }
    func startDiscovery() {
        self.bonjourBrowser.searchForServices(ofType: "_some-service._tcp.", inDomain: "local")
    }
}
extension Bonjour: NetServiceBrowserDelegate, NetServiceDelegate {
    func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) {
        discoveredService = service
        discoveredService?.delegate = self
        discoveredService?.resolve(withTimeout: 3)
    }
    func netServiceBrowser(_ browser: NetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) {
        print(errorDict)
    }
    func netServiceBrowser(_ browser: NetServiceBrowser, didRemove service: NetService, moreComing: Bool) {
        self.discovered.removeAll { $0.name == service.name }
    }
    func netServiceDidResolveAddress(_ sender: NetService) {
        if let data = sender.txtRecordData() {
            let dict = NetService.dictionary(fromTXTRecord: data)
            /// do stuff with txtRecord dict here and then add to discovered array.
            discoveredService = nil
        }
    }
}


I have no idea what could be causing the error and have tried enabling networking permissions and capabilities but have not been able to stop the error from occurring.

Thanks.
Answered by Systems Engineer in 619462022
I tried this out as well and received the same error on iOS 14 while using Bonjour to browse for a local network.

Setting the usage description and Bonjour service I am browsing for in the info.plist resolve this issue:

Code Block xml
<key>NSLocalNetworkUsageDescription</key>
<string>Looking for local tcp Bonjour service</string>
<key>NSBonjourServices</key>
<array>
<string>_some-service._tcp</string>
</array>


For more on this checkout this year's WWDC Video on Support local network privacy in your app.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

Note: that at the time of writing this these APIs are still in beta so please continue to review as the public release date draws near.
Accepted Answer
I tried this out as well and received the same error on iOS 14 while using Bonjour to browse for a local network.

Setting the usage description and Bonjour service I am browsing for in the info.plist resolve this issue:

Code Block xml
<key>NSLocalNetworkUsageDescription</key>
<string>Looking for local tcp Bonjour service</string>
<key>NSBonjourServices</key>
<array>
<string>_some-service._tcp</string>
</array>


For more on this checkout this year's WWDC Video on Support local network privacy in your app.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

Note: that at the time of writing this these APIs are still in beta so please continue to review as the public release date draws near.
10
Thanks. Adding the Bonjour Services to the Info.plist worked!
Thanks, but after I added the Privacy - Local Network Usage Description I was expecting to see a popup, but I've got nothing.
Is that ok ?
Adding

<key>NSLocalNetworkUsageDescription</key>
<string>Looking for local tcp Bonjour service</string>
<key>NSBonjourServices</key>
<array>
<string>remote-for-zoom.tcp</string>
</array>

to my Info.plist solved the problem but now I get this pop up that says your app would like to find and connect to devices on your local network. Looking for local tcp Bonjour service.

Can this be avoided? I have so many other apps that user Bonjour and they don’t bringt this pop up…


Thank you

Thomas
@Thomas

On iOS 14 this cannot be avoided if you want to use Bonjour. This is part of the new Local Network Privacy changes Apple rolled out this year:

Support local network privacy in your app
<https://developer.apple.com/videos/play/wwdc2020/10110/>

Also, make sure you checkout the latest FAQ information Quinn posted on the Forums regarding Local Network Privacy that does cover Bonjour as well:

<https://developer.apple.com/forums/thread/663858>


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
I have the same issue.

I have added both : "Privacy - Local Network Usage Description" & "Bonjour Services" to Info.plist

I have tried both syntaxes for Bonjour Services value as it seems to work in both ways depending on people feedback:
  • _Test._tcp

  • Test.tcp

I have tried with tcp and udp, or tcp or udp values for Bonjour Services

I still have the following error :
[MCNearbyServiceAdvertiser] Server did not publish: errorDict [{
  NSNetServicesErrorCode = "-72008";
  NSNetServicesErrorDomain = 10;
}].
Error Domain=NSNetServicesErrorDomain Code=-72008 "(null)"

on ios 14.1 and Xcode 12.1

What is wrong here ?

I have tried both syntaxes for Bonjour Services value

As Matt explained, _some-service._tcp is the correct format for the NSBonjourServices property.

Beyond that, it’s not clear why this is failing for your specific app. Please post a copy of your app’s final Info.plist. Use a code block (use triple backticks) to make it easier to read. Also, make sure to post the version from your built app, not the file you can see in Xcode.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
<key>NSLocalNetworkUsageDescription</key>
<string>Looking for local tcp Bonjour service</string>
<key>NSBonjourServices</key>
<array><string>_some-service._tcp</string></array>

The dot "." at the end of "_some-service._tcp" is not required in .plist but is only required when searching for service using [nsNetServiceBrowser searchForServicesOfType:@"_some-service._tcp." inDomain:@""];

inDomain parameter should empty to search service in all domains including local.

I hope that helps.

Bonjour discovery with NetServiceBrowser not working in iOS and iPadOS 14
 
 
Q