Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read TXT records with apple bonjour for iOS

I tried to get TXTrecords in didFindService function, but I found the value is null.

Have you any idea to resolve my problem?

- (void)netServiceBrowser:(NSNetServiceBrowser*)netServiceBrowser didFindService:(NSNetService*)service moreComing:(BOOL)moreComing {
    [self.services addObject:service];

    NSDictionary* dict = [[NSNetService dictionaryFromTXTRecordData:[service TXTRecordData]] retain];
    MyTreeNode *node = [[MyTreeNode alloc] initWithValue:[service name]];
    NSString* info = [self copyStringFromTXTDict:dict which:@"info"];
    if([info isEqualToString:@"child"]) { // info == null and dict == null??
        [treeNode addChild:node];
    }
    [info release];
    if (!moreComing) {
        [self sortAndUpdateUI];
    }
}
like image 334
developer Avatar asked Sep 06 '12 12:09

developer


2 Answers

You need to resolve the service first. In the callback you have there all you know is the service exists, but you don't have its records (neither its A nor its TXT).

You can use -resolveWithTimeout: to resolve it. You can also use -startMonitoring if you need to get notified when the TXT record is modified.

like image 195
Lily Ballard Avatar answered Sep 30 '22 18:09

Lily Ballard


I changed my didFindService function as follows (My application is based on the example of Apple "BonjourWeb") :

- (void)netServiceBrowser:(NSNetServiceBrowser*)netServiceBrowser didFindService:(NSNetService*)service moreComing:(BOOL)moreComing {
    [self.services addObject:service];
    NSInteger i,n = [self.services count];
    for(i=0;i<n;i++)
    {
        NSLog(@"%d %@",i,[[self.services objectAtIndex:i]name]);
        if (self.currentResolve) {
            [self stopCurrentResolve];
        }
        self.currentResolve = [self.services objectAtIndex:i];
        [self.currentResolve setDelegate:self];
        [self.currentResolve resolveWithTimeout:0.0];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(showWaiting:) userInfo:self.currentResolve repeats:NO];
    }

    if (!moreComing) {
        [self sortAndUpdateUI];
    }
}

I noticed in running my application, the function netServiceDidResolveAddress is called one time and it resolve only the last element of the array self.services (I have 11 services, it solves only the object that has the index 10). My problem is to have all the services resolved.

like image 28
developer Avatar answered Sep 30 '22 18:09

developer