How to read TXT records with apple bonjour for iOS

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

Do you have an idea to solve 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]; } } 
+4
source share
3 answers

My problem is solved, here is the final code:

 - (void)netServiceBrowser:(NSNetServiceBrowser*)netServiceBrowser didFindService:(NSNetService*)service moreComing:(BOOL)moreComing { [service setDelegate:self]; [service resolveWithTimeout:0.0]; [self.services addObject:service]; if (!moreComing) { [self sortAndUpdateUI]; } } 
+1
source

First you need to enable the service. In the callback, you have everything that you know, this service exists, but you do not have its records (neither its A nor its TXT).

You can use -resolveWithTimeout: to fix it. You can also use -startMonitoring if you need to be notified when the TXT record is changed.

+7
source

I changed the didFindService function as follows (my application is based on the Apple example "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 that when starting my application, the netServiceDidResolveAddress function is called once and only allows the last element of the self.services array (I have 11 services, it only solves the object with index 10). My problem is to allow all services.

+1
source

Source: https://habr.com/ru/post/1432715/


All Articles