I am using NSNetServiceBrowser and is able to find service list published by AVAHI in "didFindService" which looks like : Service appeared: local. _https._tcp. TEMP-Mobileyes5-1C497B9ED382 -1 Service appeared: local. _https._tcp. TEMP-Mobileyes5-1C497B8E3916 -1 Service appeared: local. _https._tcp. TEMP-Mobileyes5-1C497B9ED380 -1
But not able to find IP Address and Port number of same service. I have found that code is not reaching in "netServiceDidResolveAddress" My Code is :
class ServiceDiscovery : NSObject, NSNetServiceBrowserDelegate,NSNetServiceDelegate {
var _browser:NSNetServiceBrowser!
var _service: NSNetService!
var services = [NSNetService]()
override init() {
_browser = NSNetServiceBrowser()
super.init()
_browser.delegate = self
_browser.includesPeerToPeer = true
_browser.searchForServicesOfType("_https._tcp.", inDomain: "local.")
_browser.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
}
func netServiceBrowser(browser: NSNetServiceBrowser, didFindDomain domainString: String, moreComing: Bool) {
print(domainString)
}
func netServiceBrowser(aNetServiceBrowser: NSNetServiceBrowser, didFindService aNetService: NSNetService, moreComing: Bool) {
print("Service appeared: \(aNetService)")
services.append(aNetService)
aNetService.delegate = self
aNetService.resolveWithTimeout(5.0)
}
func netServiceBrowser(browser: NSNetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) {
print(errorDict)
}
func netServiceBrowser(browser: NSNetServiceBrowser, didRemoveService service: NSNetService, moreComing: Bool) {
print("Service removed: \(service)")
}
func netService(sender: NSNetService, didNotResolve errorDict: [String : NSNumber]) {
print(errorDict)
}
func netServiceDidResolveAddress(sender: NSNetService) {
print(sender.addresses![0])
}
}
The possible reason for your problem could be that you are calling the
inside another function, so as soon as
finishes, the local variable aNetService gets destroyed (as it is a local variable for didFindService function)
SOLUTION
I see that you've already defined a variable at the top with class-wide scope named "_service" Hence, use that by using
inside your didFindService this should solve your problem. And, netServiceDidResolveAddress should get called now.