NSNetService's TXTRecordData nil after resolution

1.2k Views Asked by At

So, here is my problem: My iOS app is advertising an NSNetService over the local network, service discovery is working good. I also know that I need to resolve my service before I am able to read it's TXTRecordData. In the service's -didPublish delegate method I made sure that my TXTRecordData is set up with an NSDictionary. So far, so good...

- (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);
}

If the -netServiceDidResolveAdress method gets called the app crashes because sender.TXTRecordData is nil. But why is it nil?

Thanks in advance for any of your answers :D

2

There are 2 best solutions below

0
On

I can't speak to why TXTRecordData is nil, but you should be using +[NSNetService dictionaryFromTXTRecordData:] to get a NSDictionary instead of NSKeyedUnarchiver to get a NSString.

0
On

It appears that the TXT record data arrives separate from the "service available" notification. Inside of my netServiceBrowser:didFindService:moreComing: method I confirmed that the incoming NSNetService has an empty TXT record even though I attached a TXT record to it before publishing, but at some time later I received a netService:didUpdateTXTRecordData: call.