英文:
Swift NWBrowser Bonjour service error NWBrowser has no member Service
问题
我正在尝试使用NWBrowser来提供网络上设备的列表。我希望获取端口号和IP地址。我遇到了错误,不确定该怎么做。
错误:
- 类型 'NWBrowser' 没有成员 'Service'
- 无法将类型 '[Any]' 的值转换为预期的参数类型 'NWBrowser.Descriptor'
import Foundation
import Network
class BonjourService {
private let serviceType: String
private let domain: String
private var browseQuery: NWBrowser?
private var services: [NWEndpoint] = []
init(serviceType: String, domain: String) {
self.serviceType = serviceType
self.domain = domain
}
func scan(completion: @escaping ([NWEndpoint]) -> Void) {
let parameters = NWParameters()
let service = NWBrowser.Service(name: serviceType, type: "_\(serviceType)._tcp", domain: domain)
browseQuery = NWBrowser(for: [service], using: parameters)
browseQuery?.browseResultsChangedHandler = { [weak self] results, changes in
self?.services = results.map { $0.endpoint }
completion(self?.services ?? [])
}
browseQuery?.start(queue: .main)
}
func stopScan() {
browseQuery?.cancel()
}
}
请注意,我已经将代码中的 HTML 实体转义符号(如 "
和 >
)还原为正常的代码格式。
英文:
I am trying to use NWBrowser to provide me list of devices on the network. I am hoping to get the port number and ip address. I am getting error and not sure what to do.
errors:
- Type 'NWBrowser' has no member 'Service'
- Cannot convert value of type '[Any]' to expected argument type 'NWBrowser.Descriptor'
import Foundation
import Network
class BonjourService {
private let serviceType: String
private let domain: String
private var browseQuery: NWBrowser?
private var services: [NWEndpoint] = []
init(serviceType: String, domain: String) {
self.serviceType = serviceType
self.domain = domain
}
func scan(completion: @escaping ([NWEndpoint]) -> Void) {
let parameters = NWParameters()
let service = NWBrowser.Service(name: serviceType, type: "_\(serviceType)._tcp", domain: domain)
browseQuery = NWBrowser(for: [service], using: parameters)
browseQuery?.browseResultsChangedHandler = { [weak self] results, changes in
self?.services = results.map { $0.endpoint }
completion(self?.services ?? [])
}
browseQuery?.start(queue: .main)
}
func stopScan() {
browseQuery?.cancel()
}
}
答案1
得分: 0
这里存在一些不正确的假设。
-
NWBrowser 专门用于浏览由一组Bonjour服务类型字符串指定的Bonjour服务。请参考苹果的NWBrowser和NSBonjourServices。这基本上意味着在浏览它们之前,您需要知道您要浏览的服务的名称。您不能使用这种方法浏览网络上设备的通用列表。
-
描述符是一个枚举,用于描述要浏览的Bonjour服务的类型。请参阅:NWBrowser.Descriptor。如果您没有使用NWListener主动广播服务,那么这不会对您有所帮助。
如果您想要查找您网络上设备的IP地址列表,您将需要切换到不同的网络框架。苹果开发者论坛上的Quinn在这里提到了一些您可以尝试的途径:从NWBrowser获取IP和端口以及发现特定设备的IP。这两个帖子都明确表示,使用NWBrowser不是这样做的方式。
英文:
There's a few incorrect assumptions here.
-
NWBrowser is specifically meant to browse for Bonjour services specified by an array of Bonjour service type strings. See Apple's NWBrowser and NSBonjourServices. This basically means you need to know what the name of the services you are browsing for are before you browse for them. You can't browse for a generic list of devices on a network using this method.
-
The descriptor is an enum describing the type of bonjour service you want to browse for. See: NWBrowser.Descriptor. If you aren't actively advertising a service using an NWListener then this isn't going to help you.
If you want to find a list of ip addresses of devices on your network you'll have to switch to a different network framework. Quinn on the Apple developer forums has mentioned some avenues you can approach here IP & Port from NWBrowser and here Discovering a specific device's ip. Both posts firmly state that using NWBrowser is not the way to do this though.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论