NSNetService工作正常,但无法获取hostName,解决导致错误 [英] NSNetService works fine, but can't get hostName, resolve causes error

查看:1048
本文介绍了NSNetService工作正常,但无法获取hostName,解决导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Bonjour和NSNetService从我的iPhone发布服务器。这一切都按预期工作,我可以浏览我正在服务的页面等。但是,在iPhone上我想显示主机名(即URL,如myDevice.local。),这样一个人也可以进入在浏览器中手动设置地址(对于缺少bonjour发现服务的客户非常有用)。我的理解是调用方法[myNetService hostName]应该给我那个地址。但是,此调用始终返回nil。我在一些论坛上读到我首先应该解析该服务,但[myNetService resolve]和[myNetService resolveWithTimeout:10]都调用委托方法

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;

错误

{
    NSNetServicesErrorCode = -72003;
    NSNetServicesErrorDomain = 10;
}

这显然意味着它已经解决了。同样,当我可以使用该服务时,所有这一切都在发生。我也可以获得端口,域和服务类型。另一个奇怪的事情是调用[myNetService addresses]返回一个空数组。

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.

我正在使用SDK 3.1.3。有没有人知道我可能做错了什么?

I'm using SDK 3.1.3. Does anybody have an idea what I could be doing wrong?

推荐答案

你可以通过[[NSProcessInfo processInfo] hostName]获取主机名没有解决刚刚发布的网络服务。

You can get the hostname via [[NSProcessInfo processInfo] hostName] without resolving the netservice you just published.

这篇关于NSNetService工作正常,但无法获取hostName,解决导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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