0

我正在做一个应用程序。我正在使用发布服务NsNetservice并使用NSNetserviceBrowser. 但有时服务成功发布服务,但浏览器无法使用didFindservice委托方法找到该服务。只有netServiceBrowserWillSearch触发,didNotsearch委托方法也没有触发。如果我重新启动设备,那么它就可以正常工作。

请帮我解决这个问题。浏览器和服务的代码请在下面查看

浏览器:

  self.browser = [[NSNetServiceBrowser alloc] init];
self.browser.includesPeerToPeer = YES;
[self.browser setDelegate:self];
[self.browser searchForServicesOfType:[NSString stringWithFormat:@"%@%@._tcp.",serviceName,Name] inDomain:@""];

服务:

  self.netService = [[NSNetService alloc] initWithDomain:@"" type:[NSString stringWithFormat:@"%@%@._tcp.",serviceName,Name] name:Name port:randomPortNumber];
self.netService.includesPeerToPeer = YES;
[self.netService setDelegate:self];
 [self.netService scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.netService publishWithOptions:NSNetServiceListenForConnections];
4

0 回答 0