In TicTacToe example two modifications were added:
2. browser.browseResultsChangedHandler was updated with code to get the txt record:
With the help of Discovery.app I can see that tictactoe service with the txt record ("key1=value1") was successfully published, but NWBrowser returns .none for metadata instead of the TXT record object.
How can I get the TXT record from the published Bonjour service?
txtRecordObject was added to the listener:
Code Block swift listener.service?.txtRecordObject = NWTXTRecord(["key1": "value1"])
2. browser.browseResultsChangedHandler was updated with code to get the txt record:
Code Block swift browser.browseResultsChangedHandler = { results, changes in results.forEach { device in switch device.metadata { case .bonjour(let record): print(record) case .none: print("none") default: break } } self.delegate?.refreshResults(results: results) }
With the help of Discovery.app I can see that tictactoe service with the txt record ("key1=value1") was successfully published, but NWBrowser returns .none for metadata instead of the TXT record object.
How can I get the TXT record from the published Bonjour service?
Try using the following to see if this improves your situation:
Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Code Block /* Listener Side */ let txtData = NWTXTRecord(["0x09": "key=value", "0x08":"paper=A4", "0x07":"passreq"]) listener.service = NWListener.Service(name: self.name, type: "_tictactoe._tcp", domain: "local", txtRecord: txtData ) /* Browser Side */ let browser = NWBrowser(for: .bonjourWithTXTRecord(type: "_tictactoe._tcp", domain: nil), using: parameters) browser.browseResultsChangedHandler = { results, changes in results.forEach{ device in print("Device metadata: \(device.metadata)") switch device.metadata { case .bonjour(let record): print("Record: \(record.dictionary)") case .none: print("Record: none") @unknown default: print("Record: default") } } }
Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com