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:
However, I've experienced the following issues:-
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!
For example, iPhone and iPad devices use Bonjour to discover AirPrint-compatible printers and other devices, and Mac computers use Bonjour to discover AirPlay-compatible devices such as Apple TV. Some apps also use Bonjour for peer-to-peer collaboration and sharing.
Bonjour is the Apple standards-based network technology designed to help devices and services discover each other on the same network.
Bonjour is pre-installed in all Apple devices including AppleTV, iOS phones, tablets, Macs and AirPort networking devices. Mac compatible-wireless printers also have Bonjour signal ports to connect to devices with the software installed.
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With