iOS上的NSNetServiceBrowser / Bonjour问题 [英] NSNetServiceBrowser/Bonjour issues on iOS

查看:405
本文介绍了iOS上的NSNetServiceBrowser / Bonjour问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我正在开发的iOS项目开发的应用程序中使用Bonjour(确切地说是NSNetServiceBrowser)而不是WiFi。

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.

然而,尽管注意到为什么会出现这些问题NSNetServiceBrowser在iPhone OS中找到未发布的服务?我仍然遇到NSNetSericeBrowser的一些困难。

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.

我的设置如下:


  • 使用运行iOS 4.5.3的iPad。

  • 使用AirPort Express作为接入点/路由器。

  • 在连接到网络的Mac上运行dns-sd -B _serviceName工作正常 - 也就是说,所有发布和停止的调用都会立即反映在桌面上。

但是,我遇到了以下问题: -

However, I've experienced the following issues:-


  1. 为a发布的服务很长一段时间(约。 15分钟+)往往不会被客户删除,即使它们从Mac上消失了!(即不会调用didRemoveService)。

  2. 有时发布的服务网络上的其他人看不到,即使它们出现在Mac上!(即未调用didFindService) - 这可能与下面的3/4点相关...如果应用程序在iPad连接到网络之前开始运行(和搜索),然后它不会正确搜索新网络。

  3. 如果设备在发布后更改网络会发生什么? / em>服务?我发现Bonjour 在新网络上重新发布服务 - 如何强制这种情况发生?

  4. 如果设备会发生什么? 搜索服务时更改网络?我发现这也会导致问题,并且往往会导致找不到会话(未调用didFindService)。

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

我遇到的问题很奇怪,因为Bonjour 90%的时间都在工作......只有10%会破坏我的应用程序的可靠性!令人讨厌的是,通过双击主页按钮并强制关闭应用程序,然后重新运行它,可以解决上述所有问题。在再次运行之后,它总是找到正确的会话或正确发布。

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.

避免这个问题的一种方法是在无线网络时拆除并重新启动NSNetServiceBrowser改变了...但这似乎非常混乱和不必要。令人沮丧的是,Bonjour是一个高级API,没有任何实际的调整和测试选项。

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.

任何想法我如何能够解决问题1-4 ?

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

谢谢!

推荐答案

好的,我设法解决了这个问题 - 如果其他人遇到Bonjour / NSNetServiceBrowser的这类可靠性问题,这是我的解决方案:

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。按如下方式安排NSNetService和NSNetServiceBrowsers:

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

2。确保从NSNetServiceBrowserDelegate实现netServiceBrowser:didNotSearch:方法。

当NSNetService无法发布时(无论出于何种原因),这将被触发。我所做的是在其中放置一个UIAlertView,向用户显示一条消息,说无法完成搜索并且应该检查他们的网络连接。浏览似乎一旦能够这样做就会自动重新启动(但我需要调查一下,我的代码中的某些东西可能会让它发生......)

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

这篇关于iOS上的NSNetServiceBrowser / Bonjour问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆