我正在向本地网络上的电话宣传桌面客户端。我的手机是 6.0.2,Xcode 是 4.5.2。
我知道桌面应用程序注册成功,因为我手机上的 Discovery 应用程序(由 Tildesoft 提供)在网络上显示了我的服务(这也排除了 wifi 问题)。
我已经下载了 Apple 应用程序示例Bonjour Web。查找“_myservice._tcp”的委托从未触发,但启动浏览方法确实触发了。
我尝试使用HHServices(它包装了 DNSService),当启动浏览方法被触发时,服务发现方法没有被触发。
在我自己的应用程序中,我尝试使用 NSNetServiceBrowser。我设置了委托,我的“netServiceBrowserWillSearch”委托方法被触发,但没有其他任何反应。
如果您想验证,请附上代码。
标题:
#import <UIKit/UIKit.h>
@interface ClientFinder_ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate,NSNetServiceBrowserDelegate>
@property (weak, nonatomic) IBOutlet UITableView *availableClientsTableView;
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindDomain:(NSString *)domainString moreComing:(BOOL)moreComing;
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing;
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didNotSearch:(NSDictionary *)errorDict;
-(void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)aNetServiceBrowser;
-(void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)aNetServiceBrowser;
@end
相关实施:
- (void)viewDidLoad
{
[super viewDidLoad];
dictionaryFoundClients = [[NSMutableDictionary alloc] initWithCapacity:1];
[self addLogoToNabar];
[self listen];
}
-(void)listen{
NSNetServiceBrowser *serviceBrowser = [NSNetServiceBrowser new];
[serviceBrowser setDelegate:self];
[self.netServiceBrowser searchForServicesOfType:TYPE inDomain:domain];
}
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindDomain:(NSString *)domainString moreComing:(BOOL)moreComing{
NSLog(@"aNetServiceBrowser didFindDomain");
}
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing{
NSLog(@"aNetServiceBrowser didFindService");
[dictionaryFoundClients setObject:aNetService forKey:aNetService.hostName];
NSLog(@"Found service: %@ @ %@", aNetService.name, aNetService.hostName);
}
-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didNotSearch:(NSDictionary *)errorDict{
NSLog(@"aNetServiceBrowser didNotSearch. Errors enumerated");
for(int a=0; a< errorDict.count; a++){
NSString *key = [[errorDict allKeys] objectAtIndex:a];
NSString *val = [errorDict objectForKey:key];
NSLog(@"%@: %@", key, val);
}
}
-(void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)aNetServiceBrowser{
NSLog(@"netServiceBrowserDidStopSearch");
}
-(void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)aNetServiceBrowser{
NSLog(@"netServiceBrowserWillSearch");
}