NSNetServiceBrowser/Bonjour issues on iOS
Asked Answered
Z

1

12

I'm using Bonjour (NSNetServiceBrowser, to be precise) over WiFi in an app I'm developing for an iOS project I've been working on.

However, despite noting the issues raised in the excellent response at Why does NSNetServiceBrowser find unpublished services in iPhone OS? I am still encountering a number of difficulties with NSNetSericeBrowser.

My set-up is as follows:

  • Working with iPads running iOS 4.5.3.
  • Using an AirPort Express as an access point/router.
  • Running dns-sd -B _serviceName on my Mac connected to the network works fine -- that is to say, all calls to publish and stop are immediately reflected on the desktop.

However, I've experienced the following issues:-

  1. Services that are published for a long time (approx. 15mins+) tend to not get removed on clients even though they disappear from the Mac! (i.e. didRemoveService doesn't get called).
  2. Sometimes services that are published are not seen by others on the network, even though they appear on the Mac! (i.e. didFindService not called) -- this is probably related to the points 3/4 below... whereby if the app starts running (and searching) before the iPad is connected to the network, then it doesn't search the new network properly.
  3. What happens if a device changes network after publishing a service? I've found that Bonjour does not re-publish the service on the new network -- how can one force this to happen?
  4. What happens if a device changes network while searching for services? I've found that this causes problems too, and it tends to result in the session not being found (didFindService not called).

The problems I've been having are weird, because Bonjour works 90% of the time... it's just that 10% that are ruining the reliability of my app! The annoying this is that ALL of the above issues can be resolved by double-pressing the home button and force-closing the app, and then re-running it. After running it again, it ALWAYS finds the correct sessions or publishes properly.

One way to sidestep this issue would be to tear-down and restart the NSNetServiceBrowser whenever the wireless network is changed... but this seems very messy and unnecessary. The frustrating thing is that Bonjour is a high-level API without any real options to tweak and test.

Any ideas how I might be able to sort out issues 1-4?

Thanks!

Zoraidazorana answered 4/10, 2011 at 9:11 Comment(1)
Don't understand why this question hasn't received more attention, I have similar problems and its reaally irritating.Dott
Z
7

OK, I managed to solve it -- and in case anyone else is experiencing these kinds of reliability issues with Bonjour/NSNetServiceBrowser, here is my solution:

1. Schedule both your NSNetService and NSNetServiceBrowsers as follows:

[netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[netServiceBrowser scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

2. Make sure you implement the netServiceBrowser:didNotSearch: method from NSNetServiceBrowserDelegate.

This will get fired when the NSNetService couldn't publish (for whatever reason). What I did was put a UIAlertView in there to display a message to the user saying that the search couldn't be completed and that they should check their network connection. Browsing seems to automatically start again once it's able to do so (but I need to look into this, maybe something in my code is making it happen...)

Zoraidazorana answered 6/10, 2011 at 10:43 Comment(3)
As a follow-up, it seems that Bonjour has been overhauled in iOS 5 so there may well be a different set of issues to now be considered.Zoraidazorana
Thanks, in fact I found that netServiceBrowser:didNotSearch: was firing every time the app came back from background. So all I do in that method is just start browsing again without any UIAlertView. Have been running it like this for some months now with no ill effects so far...Zoraidazorana
Were you able to conclude anything regarding browsing starting automatically? Specifically, I need to know when the browser starts. Currently I don't get any event whatsoever if the browser starts spontaneously. That is: start -> turn adapter off (no event) -> turn adapter on (no event) -> browsing starts by itself. I need to listen to this lifecycleGlandule

© 2022 - 2024 — McMap. All rights reserved.