NSNetServiceBrowser/Bonjour issues on iOS

前端 未结 1 1675
眼角桃花
眼角桃花 2021-01-02 01:30

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 iss

相关标签:
1条回答
  • 2021-01-02 02:00

    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...)

    0 讨论(0)
提交回复
热议问题