Reputation: 1022
I am creating an application for Peer-to-Peer Chatting using Apple's Bonjour in Swift 3.
After publishing a NetService I want to add a user_id and name parameters as TXT record. I have spent 3 - 4 hours on searching how to create a TXT record in Swift 3, but didn't find any useful solution. Once I set TXT record for a NetService, I also want to read it
For setting the TXT record data we have to use func setTXTRecord(Data?)
Please suggest me how I can Set and Get TXT record of a NetService
Upvotes: 1
Views: 1547
Reputation: 6451
Try this. These are NetServiceDelegate calls.
public func netServiceDidPublish(_ service: NetService) {
let txtDict = ["peerID": _localNode.peerID.data(using: String.Encoding.utf8)!]
let txtData = NetService.data(fromTXTRecord: txtDict)
if !service.setTXTRecord(txtData) {
print("did not set txtRecord")
}
}
public func netService(_ service: NetService, didUpdateTXTRecord data: Data) {
let txtDict = NetService.dictionary(fromTXTRecord: data)
if let idObject = txtDict["peerID"], let peerID = NSString(data: idObject, encoding: String.Encoding.utf8.rawValue) as String? {
// use peerID
}
}
If you like, you can see this in action from my project on GitHub: https://github.com/AaronBratcher/ALBPeerConnection.
Upvotes: 3