iOS 上的 NSNetServiceBrowser/Bonjour 问题

发布于 2024-12-07 23:06:14 字数 1227 浏览 1 评论 0原文

我正在为我一直在开发的 iOS 项目开发的应用程序中通过 WiFi 使用 Bonjour(准确地说是 NSNetServiceBrowser)。

然而,尽管注意到 为什么NSNetServiceBrowser 在 iPhone 操作系统中找到未发布的服务? 我在使用 NSNetSericeBrowser 时仍然遇到许多困难。

我的设置如下:

  • 使用运行 iOS 4.5.3 的 iPad。
  • 使用 AirPort Express 作为接入点/路由器。
  • 在连接到网络的 Mac 上运行 dns-sd -B _serviceName 效果很好——也就是说,所有发布和停止的调用都会立即反映在桌面上。

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

  1. 发布很长时间(大约 15 分钟以上)的服务往往不会在客户端上删除即使它们从 Mac 中消失!(即 didRemoveService 不会不会被叫到)。
  2. 有时,发布的服务不会被网络上的其他人看到,即使它们出现在 Mac 上!(即 didFindService 未调用)——这可能与下面的第 3/4 点有关。如果应用程序在 iPad 连接到网络之前开始运行(并搜索),则它无法正确搜索新网络。
  3. 如果设备在发布服务后更改网络,会发生什么情况?我发现 Bonjour 确实在新网络上重新发布该服务 - 如何强制这种情况发生?
  4. 如果设备在搜索服务时更改网络,会发生什么情况?我发现这也会导致问题,并且往往会导致找不到会话(未调用 didFindService)。

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

回避这个问题的一种方法是每当无线网络发生变化时就拆除并重新启动 NSNetServiceBrowser...但这看起来非常混乱且不必要。令人沮丧的是,Bonjour 是一个高级 API,没有任何实际选项可供调整和测试。

我有什么想法可以解决问题 1-4 吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2024-12-14 23:06:14

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

1。按如下方式安排您的 NSNetService 和 NSNetServiceBrowsers:

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

2.确保您实现了 NSNetServiceBrowserDelegate 中的 netServiceBrowser:didNotSearch: 方法。

当 NSNetService 无法发布(无论出于何种原因)时,该方法将会被触发。我所做的就是在其中放置一个 UIAlertView 来向用户显示一条消息,说明搜索无法完成,他们应该检查其网络连接。一旦能够执行此操作,浏览似乎就会自动重新开始(但我需要调查这一点,也许我的代码中的某些内容正在使其发生......)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文