所以,这是我的问题:我的iOS应用正在通过本地网络发布NSNetService,服务发现运行良好。我也知道在读取TXTRecordData之前,我需要解决我的服务。在服务的-didPublish委托方法中,我确保使用NSDictionary设置了TXTRecordData。到目前为止,一切都很好...

- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
           didFindService:(NSNetService *)aNetService
               moreComing:(BOOL)moreComing
{
    if (!self.netServicesForResolution)
        self.netServicesForResolution = [[NSMutableArray alloc] init];

    NSLog(@"serviceBrowser found service: %@, with TXTRecordData: %@",
          aNetService, aNetService.TXTRecordData);
    [self.netServicesForResolution addObject:aNetService];
    if (!moreComing) [self resolveNetServicesInArray];
}

- (void)resolveNetServicesInArray
{
    for (NSNetService *service in self.netServicesForResolution) {
        service.delegate = self;
        [service resolveWithTimeout:5];
    }
}

- (void)netServiceDidResolveAddress:(NSNetService *)sender
{
    [self.netServicesForResolution removeObject:sender];
    NSString *dataDict = [NSKeyedUnarchiver unarchiveObjectWithData:sender.TXTRecordData];
    NSLog(@"freshly resolved netService has TXTRecordData: %@", dataDict);
}

如果调用-netServiceDidResolveAdress方法,则应用程序崩溃,因为sender.TXTRecordData为nil。但是为什么没有呢?

预先感谢您的任何回答:D

最佳答案

看来TXT记录数据是与“服务可用”通知分开到达的。在我的netServiceBrowser:didFindService:moreComing:方法内部,即使在发布之前我将TXT记录附加到了它,我仍然确认传入的NSNetService拥有空的TXT记录,但是在一段时间之后,我收到了netService:didUpdateTXTRecordData:调用。

10-08 05:55