几天来,我试图实现一个在网络中浏览Bonjour服务并在控制台中显示它们的应用程序(第一步(。我用NSNetServiceBrowser
实现了一个类,并对它进行了初始化,如本文中的许多文章所示。但是在找到服务之后,我无法解析它以使主机名对我来说是可访问的。服务解析总是导致错误
-72004(NSNetServicesBadArgumentError(。
我试图使用@"local.">作为域或我的特定服务来查找,而不是使用>@"_services._dns-sd._udp."> 我在网上搜索了好几个小时,但没有找到任何关于错误的提示-72004。使用 有人知道它出了什么问题吗?//
// Bonjour.m
// BonjourTest
//
// Created by PellePe on 21.07.18.
// Copyright © 2018 PellePe. All rights reserved.
//
#import "Bonjour.h"
#import <Foundation/Foundation.h>
#include <netinet/in.h>
#define SERVICE_DOMAIN @"local"
#define SERVICE_TYPE @"_bonjourTest._tcp"
#define SERVICE_NAME @"Bonjour Test"
@interface Bonjour () <NSNetServiceDelegate, NSNetServiceBrowserDelegate>
@property (nonatomic, strong) NSMutableArray<NSNetService *> *detectedServices;
@property (nonatomic, strong) NSNetServiceBrowser *netServiceBrowser;
@end
@implementation Bonjour
#pragma mark -
#pragma mark Service Browsing
- (BOOL)isBrowsing {
return self.netServiceBrowser != NULL;
}
- (void)startServiceBrowsing:(NSString *)type {
if (self.detectedServices == NULL) {
self.detectedServices = [[NSMutableArray<NSNetService*> alloc] init];
} else {
[self.detectedServices removeAllObjects];
}
self.netServiceBrowser = [[NSNetServiceBrowser alloc] init];
self.netServiceBrowser.includesPeerToPeer = YES;
self.netServiceBrowser.delegate = self;
[self.netServiceBrowser searchForServicesOfType: type // SERVICE_TYPE
inDomain: @""];
}
- (void)stopServiceBrowsing {
assert(self.netServiceBrowser != NULL);
[self.netServiceBrowser stop];
self.netServiceBrowser = NULL;
}
#pragma mark -
#pragma NSNetServiceDelegate
-(void)netServiceWillResolve:(NSNetService *)sender {
NSLog(@"netService will resolve - %@", sender.hostName);
}
- (void)netService:(NSNetService *)sender
didNotResolve:(NSDictionary<NSString *,NSNumber *> *)errorDict {
NSLog(@"netService did not resolve");
for (NSString *key in [errorDict allKeys]) {
NSLog(@"%@: %@", key, [errorDict objectForKey: key]);
}
}
- (void)netServiceDidResolveAddress:(NSNetService *)sender {
NSLog(@"netService did resolve address - %@", sender.hostName);
}
#pragma mark -
#pragma mark NSNetServiceBrowserDelegate
- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)browser {
NSLog(@"Browser will search");
}
- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)browser {
NSLog(@"Browser did stop search");
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser didFindService:(NSNetService *)service moreComing:(BOOL)moreComing {
[self.detectedServices addObject: service];
service.delegate = self;
[service resolveWithTimeout: 100];
NSLog(@"Find service: %@ - %@ - %@ (%@)", service.name, service.hostName, service.type, moreComing ? @"YES" : @"No");
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser didRemoveService:(NSNetService *)service moreComing:(BOOL)moreComing {
NSLog(@"Remove service: %@ - %@ - %@", service.name, service.hostName, service.type);
}
@end
NSNetServiceBrowser
对如何初始化服务没有影响。
所以这里的问题是,当你搜索_services._dns-sd._udp.
时,你不会得到实际的服务,但你会得到所有类型的服务,例如。
let browser = NetServiceBrowser()
browser.searchForServices(ofType: "_googlecast._tcp", inDomain: "local")
此查询将返回类似于此的服务
NetService(domain: "local", type: "_googlecast._tcp", name: "RoomTV")
这是有效的,你可以毫无问题地解决,但当你像这样浏览时:
browser.searchForServices(ofType: "_services._dns-sd._udp.", inDomain: "local")
返回的服务将如下所示:
NetService(domain: ".", type: "_tcp.local.", name: "_googlecast")
因此,要解决此问题,您需要使用新类型再次搜索以获得实际服务。
let type = "(service.name).(service.type)"
.replacingOccurrences(of: "local.", with: "")
browser.searchForServices(ofType: type, inDomain: "local")