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"

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