0

我正在向本地网络上的电话宣传桌面客户端。我的手机是 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");
}
4

2 回答 2

0

如果这都是本地的,则需要使用 searchForServicesOfType: 方法而不是 searchForRegistrationDomains 方法。

-(void)listen{
  NSNetServiceBrowser *serviceBrowser = [NSNetServiceBrowser new];
  [serviceBrowser setDelegate:self];
  [serviceBrowser searchForServicesOfType:@"_your_service_type._tcp" inDomain:@""];;
}

“_your_service_type._tcp”是您的桌面应用程序在宣传其服务时使用的服务类型。

于 2013-01-12T05:01:49.790 回答
0

不知道为什么它开始工作,但这是我的工作代码:

    [self stopCurrentResolve];
    [self.netServiceBrowser stop];
    [self.services removeAllObjects];

    NSNetServiceBrowser *aNetServiceBrowser = [[NSNetServiceBrowser alloc] init];
    if(!aNetServiceBrowser) {
        // The NSNetServiceBrowser couldn't be allocated and initialized.
        return NO;
    }

    aNetServiceBrowser.delegate = self;
    self.netServiceBrowser = aNetServiceBrowser;
    [self.netServiceBrowser searchForServicesOfType:type inDomain:domain];
于 2013-01-14T17:48:26.933 回答