NWListener.Service also browsable on current device

We're using an NWListener to publish a Bonjour service, and within the same app allow browsing for that service (peer-to-peer). However, when browsing, this will cause the service of the current device also to be shown. I've considered a few approaches of addressing this:

  • Checking the service name, but as I understand these could potentially collide (e.g. both just called "iPhone"). Since this name is also shown in the UI, we can't just change it to a UUID or the like.
  • Putting an identifier in the TXT data. However, this requires resolving the service before it is available, and IIRC just resolving every service is consider bad practice (for performance reasons).

Any suggestions would be appreciated.

Accepted Answer

Checking the service name

Do this.

but as I understand these could potentially collide

Such collisions are resolved in one of two ways:

  • If the new service has auto-rename enabled, it’ll rename itself out of the way.

  • If not, its registration will fail.

ps You can control the auto-rename behaviour using:

  • nw_advertise_descriptor_set_no_auto_rename
    in C
  • The

    noAutoRename
    property of
    NWListener.Service
    in Swift

The latter is not available on currently shipping systems (r. 47917524) but is available in iOS 13 beta and friends.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"
NWListener.Service also browsable on current device
 
 
Q