NSNet服务问题
我正在尝试在 iOS 设备和 Mac 之间建立 Bonjour 连接。发现彼此的效果很好,但我在 setTXTRecordData:
方面遇到了问题。它总是失败(BOOL 返回 NO)...
_serviceInstances 创建:
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
[aNetService retain];
[aNetService setDelegate:self];
[aNetService startMonitoring];
[aNetService performSelectorOnMainThread:@selector(resolve) withObject:nil waitUntilDone:YES];
[_serviceInstances addObject:aNetService];
}
发送尝试:
NSNetService*service = [_serviceInstances objectAtIndex:[servicesTable selectedRow]];
[service setDelegate:self];
NSDictionary*txtRecordDataDictionary = [NSDictionary dictionaryWithObject:@"2" forKey:@"Version"];
if (service)
{
BOOL success = [service setTXTRecordData:[NSNetService dataFromTXTRecordDictionary:txtRecordDataDictionary]];
if (!success)
{
NSRunCriticalAlertPanel(@"Sync Error", @"Failed to contact Client. Please restart Carbon on your iPad and try again.", @"OK", nil, nil);
}
NSLog(@"Service: %@",service);
}
NSLog 消息输出 Service:
这是正确的。
iOS 代码:
NSNetService*service = [[NSNetService alloc] initWithDomain:@"local." type:@"_test._tcp." name:[[UIDevice currentDevice] name] port:28];
[service setDelegate:self];
[service setTXTRecordData:nil];
[service publish];
[service startMonitoring];
- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data
{
NSLog(@"Got Data! of %@",sender);
NSPropertyListFormat format;
NSDictionary*dict = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:nil];
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:[sender name] message:[dict objectForKey:@"Version"]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[myAlert show];
[myAlert release];
}
由于某种原因,当我启动 iPad 应用程序时会调用警报(标题除外的空白内容),但不会响应来自 Mac 的任何信号。
我确定我错过了什么?
I am trying to establish a Bonjour connection between an iOS device and a Mac. Discovering each other is working great, but I am having problems with setTXTRecordData:
. It always fails (BOOL returns NO)...
_serviceInstances creation:
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
[aNetService retain];
[aNetService setDelegate:self];
[aNetService startMonitoring];
[aNetService performSelectorOnMainThread:@selector(resolve) withObject:nil waitUntilDone:YES];
[_serviceInstances addObject:aNetService];
}
Sending attempt:
NSNetService*service = [_serviceInstances objectAtIndex:[servicesTable selectedRow]];
[service setDelegate:self];
NSDictionary*txtRecordDataDictionary = [NSDictionary dictionaryWithObject:@"2" forKey:@"Version"];
if (service)
{
BOOL success = [service setTXTRecordData:[NSNetService dataFromTXTRecordDictionary:txtRecordDataDictionary]];
if (!success)
{
NSRunCriticalAlertPanel(@"Sync Error", @"Failed to contact Client. Please restart Carbon on your iPad and try again.", @"OK", nil, nil);
}
NSLog(@"Service: %@",service);
}
The NSLog message outputs Service: <NSNetService 0x441b40> local. _test._tcp. David's iPad
which is correct.
iOS Code:
NSNetService*service = [[NSNetService alloc] initWithDomain:@"local." type:@"_test._tcp." name:[[UIDevice currentDevice] name] port:28];
[service setDelegate:self];
[service setTXTRecordData:nil];
[service publish];
[service startMonitoring];
- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data
{
NSLog(@"Got Data! of %@",sender);
NSPropertyListFormat format;
NSDictionary*dict = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:nil];
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:[sender name] message:[dict objectForKey:@"Version"]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[myAlert show];
[myAlert release];
}
For some reason, the alert is called when I launch the iPad app (blank contents except for the title) but doesnt respond to any signals from my Mac.
I am sure I am missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只有服务的发布者可以设置TXT记录数据。已发布服务的发现者可以读取 TXT 记录,但无法更改它。 TXT 记录为服务发布者提供了一种发布附加的、公开可读的数据的方法,这些数据可以从 DNS 条目中读取,而无需直接联系发布者。
预计到达时间: 至少,
NSNetService
的文档表明您无需解析服务即可获取 TXT 记录数据。CFNetService
文档指出,您必须先解析该服务,然后才能获取 TXT 记录数据。CFNetService
和NSNetService
似乎都只能使用可能的 DNS-SD 记录的子集,这些记录可以使用
,它允许将多个 TXT 记录附加到单个广告服务。Only the publisher of the service can set the TXT record data. The discoverer of a published service can read the TXT record but cannot alter it. The TXT record provides a way for the publisher of the service to publish additional, publicly readable data that can be read from the DNS entry without having to contact the publisher directly.
ETA: At least,
NSNetService
's documentation makes it appear like you needn't resolve the service before you can grab the TXT record data. TheCFNetService
documentation notes that you must resolve the service before you can get the TXT record data. BothCFNetService
andNSNetService
seem to work only with a subset of the possible DNS-SD records that can be created and discovered using the C API described in<dns_sd.h>
, which allows for attaching multiple TXT records to a single advertized service.