我在解决NSNetService
时遇到了问题。当NSNetService
找到该服务时,我已成功解决NSNetServiceBrowser
。
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing{
if (![self.services containsObject:aNetService]) {
[aNetService setDelegate:self];
[aNetService resolveWithTimeout:5.0];
}
}
然后成功调用此方法
-(void)netServiceDidResolveAddress:(NSNetService *)sender{
NSArray *addresses = [ns addresses];
NSDictionary* dict = [NSNetService dictionaryFromTXTRecordData:[sender TXTRecordData]];
// Here both values are ok
}
但我想将NSNetService解析到服务器端以获取发布该服务的IP地址。
-(void)netServiceDidPublish:(NSNetService *)ns{
[ns setDelegate:self];
[ns resolveWithTimeout:5.0];
}
但是这个方法没有调用。
-(void)netServiceDidResolveAddress:(NSNetService *)sender{
}
然而我这样做了
-(void)netServiceDidPublish:(NSNetService *)ns
{
NSArray *addresses = [ns addresses]; // this gives null
// this also gives null
NSDictionary* dict = [NSNetService dictionaryFromTXTRecordData:[sender TXTRecordData]];
}
但值为null
。
答案 0 :(得分:0)
前几天也有这个问题,并花了一些时间才弄明白。 出于某种原因,我需要将aNetService分配给一个属性。
# Declare a NSNetService property in the .h or further up .m file
@property (strong, nonatomic) NSNetService *currentService;
# In the .m file.
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing{
if (![self.services containsObject:aNetService]) {
self.currentService = aNetService;
[self.currentService setDelegate:self];
[self.currentService resolveWithTimeout:5.0];
}
}