简体   繁体   English

如何使用apple bonjour for iOS阅读TXT记录

[英]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. 我试图在didFindService函数中获取TXTrecords,但我发现该值为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];
    }
}

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). 在回调中,您所知道的是服务存在,但您没有其记录(既不是A也不是TXT)。

You can use -resolveWithTimeout: to resolve it. 您可以使用-resolveWithTimeout:来解决它。 You can also use -startMonitoring if you need to get notified when the TXT record is modified. 如果需要在修改TXT记录时收到通知,也可以使用-startMonitoring

I changed my didFindService function as follows (My application is based on the example of Apple "BonjourWeb") : 我更改了我的didFindService函数,如下所示(我的应用程序基于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). 我注意到在运行我的应用程序时,函数netServiceDidResolveAddress被调用一次,它只解析数组self.services的最后一个元素(我有11个服务,它只解决索引为10的对象)。 My problem is to have all the services resolved. 我的问题是解决所有服务问题。

My problem is resolved 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];
    }
}

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

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