How to create and advertise a Bonjour service with Network.framework?

I'm trying to create and advertise a Bonjour service via Network.framework.

In Xcode, target, Info tab, I've added the entry to plist:

And then written the following code:

        guard let listener = try? NWListener(service: .init(name: "My Printer Service", type: "_printer._tcp"),using: .tcp) else {
            return nil
        }
     
        self.listener = listener
        
        listener.stateUpdateHandler = { newState in
            switch newState {
            case.ready:
                print("starting")
            case .failed(let error):
                print("error: \(error)")
            default:
                print(newState)
            }
        }
        
        listener.start(queue: .main)

However, the status is failed with error message: POSIXErrorCode(rawValue: 22): Invalid argument

I’m not sure what’s triggering this specific failure. Try running through the steps in Getting Started with Bonjour and see whether you encounter the problem there too.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

In case somebody also runs into this, I had the same issue (although I created the NWListener differently), and it turned out that the error was caused by an empty listener.newConnectionHandler. Set it before calling start, and it might fix it. Just in case I also had serviceRegistrationUpdateHandler set, although I don't know if it's required.

How to create and advertise a Bonjour service with Network.framework?
 
 
Q