Hi,
I wanted to try using the new NWBrowser available in iOS 13 to replace my old Bonjour browsing code(NetServiceBrowser), problem is I'm unabe to get all IPv6 Addresses(except link local address) of the service I'm looking for.
In my environment, when I checked with ifconfig command, there are three IPv6 addresses.
- fe80::c092:a0ff:fe51:3c8
- fd1c:efcd:7e66:0:c092:a0ff:fe51:3c8
- fd1c:efcd:7e66:0:68d:6b27:81e2:7511
Using NWBrowser and NWConnection, I ended up getting #1 address but I couldn't get #2, #3 addresses. Here is my code.
let bonjour = NWBrowser.Descriptor.bonjourWithTXTRecord(type: serviceType, domain: searchDomain)
let params = NWParameters.init()
self.nwBrowser = NWBrowser(for: bonjour, using: params)
self.nwBrowser?.browseResultsChangedHandler = { [weak self] results, changes in
guard let self = self else { return }
for change in changes {
if case .added(let result) = change {
let connection = NWConnection(to: result.endpoint, using: .udp)
connection.stateUpdateHandler = { [weak self] state in
switch state {
case .ready:
if let remoteEndpoint= connection.currentPath?.remoteEndpoint,
case .hostPort(let host, let port) = innerEndpoint {
// here I can get #1 address from host
// but, I couldn't get #2, #3 addresses
}
}
}
connection.start(queue: .main)
}
}
}
self.nwBrowser?.start(queue: .main)
Using NetServiceBrowser and netServiceDidResolveAddress(), I can get All IP addresses(#1, #2, #3) Here is my code.
let netServiceBrowser = NetServiceBrowser()
netServiceBrowser.delegate = self
netServiceBrowser.searchForServices(ofType: serviceType, inDomain: searchDomain)
func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) {
service.delegate = self
service.resolve(withTimeout: resolutionTimeoutSeconds)
}
func netServiceDidResolveAddress(_ service: NetService) {
// service.addresses is [Data], I can get all addresses I want.
}
I wonder if getting only Link Local Address through NWConnection is the intended behavior. Or is there a way to get all address using NWBrowser and NWConnection?
You are actually running a connection to the Bonjour endpoint, and so you only get a single IP address because any given connection only has a single remote IP address.
What you need here is the Bonjour ‘resolve’ operation, which is what you get when you call resolve(withTimeout:)
on NetService
. Network framework has no API for this resolve operation. See this response, and the other posts on that thread, for the details.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"