NSNetServiceBrowser delegate not called when searching
Asked Answered
D

1

5

I got a really annoying problem. Im trying to search after a NSNetService (that i know exist). I've copied the example code from http://www.macresearch.org/cocoa-scientists-part-xxviii-bonjour-and-how-do-you-do into my own application. I can see (in the example program) that my service is created. But when i hit search, and start a NSNetServiceBrowser search, nothing happens. The delegate function newer gets called

 -(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser didFindService:(NSNetService *)aService moreComing:(BOOL)more {

I've been checking stuff like that my objects are still retained, and i can't see that anything is released (and i don't have GC on). Anybody got any ideas what to do? The code is exactly the same as in the example, only i got some other stuff around it (running a openGL window).

Defend answered 19/8, 2010 at 22:4 Comment(2)
Are you sure you have assigned the right delegate to the instance of NSNetServiceBrowser?Odd
I have the same problem. But it only gets called when I publish the service and search it from the same device, but when testing it with two devices never finds the service.Donets
N
10

This question is eons old (iOS 4/5 era?) so I'm not sure if this is the answer for you, but make sure that you're using NSNetServiceBrowser from the main thread of your application only.

(I can't remember if it also must be initialized on the main thread, or if it must just be initialized and called -search on from the same thread, or if it suffices to call -search from the main thread, but I had the exact same problem and in my case it was related to using a background thread in some way. Sorry for the vague answer.)

Norahnorbert answered 25/3, 2014 at 10:42 Comment(3)
Just tested this myself, you need to init NSNetService from the main thread itself too. Just assigning the delegate and doing a call such as NSNetService's "searchForServicesOfType..." within the main thread alone won't work. I threw the netservice stuff within a dispatch_async(dispatch_get_main_queue(), ^{ ... }); block.Redpencil
Actually you don't need to use NSNetService on the main thread. You do need to make sure you call [[NSRunLoop currentRunLoop] run] on the thread you're initializing NSNetService on. The main runloop is running by default, but other threads may not be.Latricelatricia
And make sure you call -run after you start the search! Otherwise nothing will be added to the run loop, and start won't do anything.Farlay

© 2022 - 2024 — McMap. All rights reserved.