第一个问题:我正在使用这个this method来触发LocalNetwork Permission对话框,但是在对话框出现后,用户授予访问权限,没有代理触发,但是它进入就绪状态。你知道为什么吗?
第二个问题:是否有可能在不触发对话框情况下检测是否授予了LocalNetwork权限?
import Foundation
import Network
public class LocalNetworkAuthorization: NSObject {
private var browser: NWBrowser?
private var netService: NetService?
private var completion: ((Bool) -> Void)?
func requestAuthorization(completion: @escaping (Bool) -> Void) {
self.completion = completion
// Create parameters, and allow browsing over peer-to-peer link.
let parameters = NWParameters()
parameters.includePeerToPeer = true
// Browse for a custom service type.
let browser = NWBrowser(for: .bonjour(type: "_wobie._tcp", domain: nil), using: parameters)
self.browser = browser
browser.stateUpdateHandler = { newState in
switch newState {
case .failed(let error):
print("::: ->> error: \(error.localizedDescription)")
case .ready:
print("::: ->> ready")
case .cancelled:
print("::: ->> canceled")
break
case let .waiting(error):
print("::: ->> Local network permission has been denied: \(error)")
self.reset()
self.completion?(false)
default:
break
}
}
self.netService = NetService(domain: "local.", type:"_lnp._tcp.", name: "LocalNetworkPrivacy", port: 1100)
self.netService?.delegate = self
self.browser?.start(queue: .main)
self.netService?.publish()
}
private func reset() {
self.browser?.cancel()
self.browser = nil
self.netService?.stop()
self.netService = nil
}
}
extension LocalNetworkAuthorization : NetServiceDelegate {
public func netServiceDidPublish(_ sender: NetService) {
self.reset()
print("::: Local network permission has been granted")
completion?(true)
}
public func netService(_ sender: NetService, didNotResolve errorDict: [String : NSNumber]) {
print("::: error: \(errorDict)")
}
public func netService(_ sender: NetService, didAcceptConnectionWith inputStream: InputStream, outputStream: OutputStream) {
print("")
}
public func netServiceWillResolve(_ sender: NetService) {
print("")
}
}
1条答案
按热度按时间wgx48brx1#
问题是我没有在Bonjour服务中添加“_lnp._tcp.”。