NSNetService 工作正常,但无法获取主机名,解析导致错误
我使用 Bonjour 和 NSNetService 从我的 iPhone 发布服务器。一切都按预期工作,我可以浏览我正在服务的页面等。但是,在 iPhone 上我想显示主机名(即 URL,如“myDevice.local”。),以便人们也可以输入在浏览器中手动输入地址(对于缺少 bonjour 发现服务的客户端很有用)。我的理解是,调用方法 [myNetService hostName] 应该给我该地址。然而,这个调用总是返回nil。我在一些论坛上读到,我首先应该解决该服务,但是 [myNetServiceresolve] 和 [myNetServiceresolveWithTimeout:10] 都调用了
- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict;
带有错误的
{
NSNetServicesErrorCode = -72003;
NSNetServicesErrorDomain = 10;
}
委托方法,这显然意味着它已经解决了。同样,这一切都是在我可以使用该服务时发生的。我还可以获得端口、域和服务类型。唯一奇怪的事情是调用 [myNetService 地址] 返回一个空数组。
我使用的是 SDK 3.1.3。有人知道我可能做错了什么吗?
I'm using Bonjour with an NSNetService to publish a server from my iPhone. It all works as expected, I can browse the pages I'm serving etc. However, on the iPhone I want to display the host name (i.e. the URL, like "myDevice.local."), so that one can also enter the address manually in a browser (useful for clients missing a bonjour discovery service). My understanding is that calling the method [myNetService hostName] should give me that address. However, this call always returns nil. I read on some forum that I first should resolve the service, however both [myNetService resolve] and [myNetService resolveWithTimeout:10] call the delegate method
- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict;
with the error
{
NSNetServicesErrorCode = -72003;
NSNetServicesErrorDomain = 10;
}
which apparently means that it is already resolved. Again, all this is happening while i can use the service. Also I can get the port, the domain, and the type of the service. The only other strange thing is that the call [myNetService addresses] returns an empty array.
I'm using SDK 3.1.3. Does anybody have an idea what I could be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[[NSProcessInfo processInfo] hostName]
方法对我来说没有用,原因有两个:以下代码来自 http://iphoneappcode.blogspot.com/2012/05/how-to-get-device-ipaddess-in-iphone.html 最适合我,总是返回本地主机名敏感大小写:
The
[[NSProcessInfo processInfo] hostName]
method was not useful for me for two reasons:The following code from http://iphoneappcode.blogspot.com/2012/05/how-to-get-device-ipaddess-in-iphone.html worked best for me, always returning the local host name with sensitive case:
您可以通过 [[NSProcessInfo processInfo] hostName] 获取主机名,而无需解析您刚刚发布的网络服务。
You can get the hostname via [[NSProcessInfo processInfo] hostName] without resolving the netservice you just published.
查看此链接:
这表明您的错误仍在尝试解决服务:
https://developer.apple.com/library/IOs/documentation/Cocoa/Reference/Foundation/Classes/NSNetService_Class/index.html#//apple_ref/c/tdef/NSNetServicesError
Check this link out:
This says your error it was still trying to resolve the service:
https://developer.apple.com/library/IOs/documentation/Cocoa/Reference/Foundation/Classes/NSNetService_Class/index.html#//apple_ref/c/tdef/NSNetServicesError