简体   繁体   English

NSNetService 问题

[英]NSNetService Problems

I am trying to establish a Bonjour connection between an iOS device and a Mac.我正在尝试在 iOS 设备和 Mac 之间建立 Bonjour 连接。 Discovering each other is working great, but I am having problems with setTXTRecordData: .发现彼此效果很好,但我遇到了setTXTRecordData:的问题。 It always fails (BOOL returns NO)...它总是失败(BOOL 返回 NO)...

_serviceInstances creation: _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];
}

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 NSLog 消息输出Service: <NSNetService 0x441b40> local. _test._tcp. David's iPad Service: <NSNetService 0x441b40> local. _test._tcp. David's iPad Service: <NSNetService 0x441b40> local. _test._tcp. David's iPad which is correct. Service: <NSNetService 0x441b40> local. _test._tcp. David's iPad是正确的。

iOS Code: 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];
}

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.出于某种原因,当我启动 iPad 应用程序(除标题之外的空白内容)但不响应来自我的 Mac 的任何信号时,会调用警报。

I am sure I am missing something?我确定我错过了什么?

Only the publisher of the service can set the TXT record data.只有服务的发布者才能设置 TXT 记录数据。 The discoverer of a published service can read the TXT record but cannot alter it.已发布服务的发现者可以读取 TXT 记录,但不能更改它。 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. TXT 记录为服务的发布者提供了一种发布其他公开可读数据的方法,这些数据可以从 DNS 条目中读取,而无需直接联系发布者。

ETA: At least, NSNetService 's documentation makes it appear like you needn't resolve the service before you can grab the TXT record data. ETA:至少, NSNetService的文档看起来好像您不需要解析服务就可以获取 TXT 记录数据。 The CFNetService documentation notes that you must resolve the service before you can get the TXT record data. CFNetService文档指出,您必须先解析服务,然后才能获取 TXT 记录数据。 Both CFNetService and NSNetService 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. CFNetServiceNSNetService似乎都只适用于可能的 DNS-SD 记录的子集,这些记录可以使用<dns_sd.h>中描述的 C API 创建和发现,它允许将多个 TXT 记录附加到单个广告服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM