我已经实现了NEHotspotHelper
,以便我可以在后台对具有强制网络门户的网络执行身份验证。
我需要在"身份验证"状态下执行 Web 请求,以便我可以检索 Wispr 并访问 API。
但是,当我尝试使用 URLSession
发送 Web 请求时,请求失败。这是错误:
[594:88737] dnssd_clientstub read_all(7) DEFUNCT
[594:88783] TIC TCP Conn Failed [4:0x1c0177a00]: 12:8 Err(-65554)
[594:88783] Task <FFD0DAE6-4864-437D-94F2-C9ED5D5748E2>.<1> HTTP load failed (error code: -1003 [12:8])
[594:88783] Task <FFD0DAE6-4864-437D-94F2-C9ED5D5748E2>.<1> finished with error - code: -1003
查看我的代码片段:
let registered = NEHotspotHelper.register(options: options, queue: queue) { (cmd: NEHotspotHelperCommand) in
print("Received command: (cmd.commandType.rawValue)")
if cmd.commandType == NEHotspotHelperCommandType.filterScanList {
//Get all available hotspots
print("filter scan list")
var list: [NEHotspotNetwork] = cmd.networkList!
var response: NEHotspotHelperResponse
for l in list {
if (l.ssid=="my-ssid") {
response = cmd.createResponse(NEHotspotHelperResult.success)
} else {
response = cmd.createResponse(NEHotspotHelperResult.failure)
}
response.setNetworkList([chosenNetwork])
response.deliver()
}
} else if cmd.commandType == NEHotspotHelperCommandType.evaluate {
if let network = cmd.network {
if (network.ssid=="my-ssid") {
network.setConfidence(NEHotspotHelperConfidence.high)
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetwork(network)
response.deliver() //Respond back
} else {
let response = cmd.createResponse(NEHotspotHelperResult.failure)
response.deliver()
}
}
} else if cmd.commandType == NEHotspotHelperCommandType.authenticate {
print("authenticate")
var response = cmd.createResponse(NEHotspotHelperResult.unsupportedNetwork)
if let network = cmd.network{
if network.ssid == "my-ssid"{
self.queryUrl()
response = cmd.createResponse(NEHotspotHelperResult.success)
}
}
response.deliver() //Respond back
}
}
func queryUrl(){
let config = URLSessionConfiguration.default
config.allowsCellularAccess = false;
let session = URLSession.init(configuration: config)
let url = URL(string: "https://172.217.20.35")
let semaphore = DispatchSemaphore(value: 0)
let task = session.dataTask(with: url!){(data, response, error) in
if data==nil {
print(data as Any)
}
else{
print(NSString(data: data!, encoding: String.Encoding.utf8.rawValue) as Any)
}
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: .distantFuture)
}
我也面临着类似的问题。但是,我发现开发人员需要在向连接的网络发出 Web 请求之前将请求与收到的命令绑定。您需要做的就是进行NSMutableURLRequest
然后调用hitTestURLRequest.bind(to: command)
因为绑定函数是在NSMutableURLRequest
类别中定义的。
其他人报告了NEHotspotHelper
回调中的名称服务器解析问题。尝试使用 IP 地址进行呼叫。
也不要忘记URLSession
异步工作。只有在 Web 服务调用完成后,才需要调用response.deliver()
。
这就是它:
if cmd.commandType
filterScanList
- SetConfidence to each of your networks in cmd.networkList - High and add to a local list variable.
- create Response.Success
- set Response.NetworkList to your list of confident networks
----
- deliver Response
Evaluate or PresentUI
//If it's your network
- SetConfidence to cmd.network - High
- create Response.Success
- set Response.Network to cmd.network
//If it's not your network
- create Response.UnsupportedNetwork
----
- deliver Response
Authenticate or Maintain
//If it's your network
- SetConfidence to cmd.network - High
- create Response.Success
- set Response.Network to cmd.network
- TRY AUTHENTICATE HERE SYNCHRONOUSLY
//If it's not your network
- create Response.UnsupportedNetwork
----
- deliver Response
Logoff or None
- create Response.Success
----
- deliver Response