3

我已经实现了,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)    
}
4

3 回答 3

1

我也面临着类似的问题。但是,我发现开发人员需要先将请求与接收到的命令绑定,然后才能向连接的网络发出 Web 请求。您需要做的就是 makeNSMutableURLRequest然后调用hitTestURLRequest.bind(to: command),因为 bind 函数是在NSMutableURLRequest.

于 2018-06-08T04:46:55.927 回答
0

NEHotspotHelper其他人报告了来自回调的名称服务器解析问题。尝试使用 IP 地址拨打电话。

也不要忘记URLSession异步工作。response.deliver()只有在 Web 服务调用完成后,您才需要调用。

于 2018-01-26T14:08:28.000 回答
0

这是它的正义:

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
于 2018-01-26T12:20:34.243 回答