ios - NEHotspotHelperCommand 命令不调用 PresentUI 或进行身份验证

标签 ios swift captivenetwork nehotspothelper

我正在使用 NEHotspotHelper API,并成功获取扫描的 wifi 列表,但是当我尝试通过设置配置连接可用 wifi 之一(具有强制门户)时,NEHotspotHelperCommand PresentUi 命令永远不会调用。请检查我下面的代码

 var options = [String: NSObject]()
 options[kNEHotspotHelperOptionDisplayName] = NSLocalizedString("linq verified", comment: "") as NSObject?

    NSLog("Lets register", "")
    NEHotspotHelper.register(options: options, queue: DispatchQueue.main, handler: {(_ cmd: NEHotspotHelperCommand) -> Void in

        if  cmd.commandType == NEHotspotHelperCommandType.filterScanList {
            // scane list

            if cmd.networkList != nil {
                self.networkList = []
                var i2e1Network : [NEHotspotNetwork] = []
                for network: NEHotspotNetwork in cmd.networkList! {

                    NSLog("Found network \(network.bssid) with \(network.ssid)", "")
                    self.networkList.append(network)

                    if (WifiHelper.isI2E1Network(network: network)){
                        network.setConfidence(.high)
                        i2e1Network.append(network)
                    }
                }

                let response = cmd.createResponse(NEHotspotHelperResult.success)
                response.setNetworkList(i2e1Network)
                response.deliver()


                // categories all the network
                self.categoriesNetworkList()

                // +1 is for header and the current wifi cell
                self.numberOfCells = self.easyConnectNetworkList.count + self.otherNetworkList.count + self.swAppNetworkList.count + self.headerPosition + 1

                // after fetching the list of available wifi set the UI
                self.setUI()
            }else if cmd.network != nil{
                let resp : NEHotspotHelperResponse = cmd.createResponse(NEHotspotHelperResult.success)
                print(resp)

            }
        }
        else if cmd.commandType == NEHotspotHelperCommandType.evaluate{
            // evalution
            print("evaluting the network")

            cmd.network?.setConfidence(.high)
            let response = cmd.createResponse(NEHotspotHelperResult.success)
            response.setNetwork(cmd.network!)
            response.deliver()
            self.checkCaptive(cmd: cmd)

        }
        else if cmd.commandType == NEHotspotHelperCommandType.authenticate{
            // authentication
            print("authenticating the network")
            print("network::"+(cmd.network?.ssid)!)
        }else if cmd.commandType == NEHotspotHelperCommandType.presentUI{
             print("presentUI")

        }else if cmd.commandType == NEHotspotHelperCommandType.maintain{
             print("maintain")
        }

    })

当有移动网络可用时,我还有一个问题,并且我尝试检查强制门户是否始终返回成功。请同时检查以下代码

func checkCaptive(cmd: NEHotspotHelperCommand){
        let url = URL(string: "http://captive.apple.com/hotspot-detect.html")

    let request = NSMutableURLRequest(url: url!)
    request.httpMethod = "GET" //GET method
    request.bind(to: cmd)
    request.allowsCellularAccess = false
    request.cachePolicy = .reloadIgnoringCacheData
    let task = URLSession.shared.dataTask(with: request as URLRequest) { //Block for the response
        data, response, error in

        if error != nil { //Error request
            print("error : \(error)")
            return
        }else{
            let html = String(data: data!, encoding: String.Encoding.utf8)
            print(html)

            if (!(html?.contains("Success"))!){
                let storyboard = UIStoryboard(name: "GetWifi", bundle: nil)
                let captiveLoginVC = storyboard.instantiateViewController(withIdentifier: "CaptivePortalLoginViewController") as! CaptivePortalLoginViewController
                                    self.navigationController?.pushViewController(captiveLoginVC, animated: true)

            }

        }
    }
    task.resume()

}

最佳答案

首先响应 NEHotspotHelperCommandType.evaluate 命令的 HotspotHelper 将会调用 NEHotspotHelperCommandType.presentUI,因此我们所做的就是从评估命令中删除大部分代码并尽快响应。现在它对我们来说工作得很好。

关于ios - NEHotspotHelperCommand 命令不调用 PresentUI 或进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49629194/

相关文章:

iphone - 刷新引用 ManagedObjectContext

ios - 为什么授权 header 在 iOS PATCH 请求中被删除?

ios - 当尝试获取应用程序方向时,“statusBarOrientation”在 iOS 13.0 中已被弃用

ios - wifi Captive Portal下的iOS自动登录

android - CSS 背景颜色和图像冲突

ios - AFOAuth2Client 无法访问资源

iphone - 为什么我的CAGradientLayer为所有更改设置动画?

swift - 播放一个声音循环,同时至少有一个同类 Sprite 在舞台上

javascript - 强制门户上的权限弹出窗口

iPhone : how to auto connect to a known wifi SSID?