Search code examples
iosswiftcaptivenetworknehotspothelper

NEHotspotHelperCommand command not calling presentUI or authenticate


I am using NEHotspotHelper API, and successfully able to get the list of scanned wifi, but when I try to connect one of the available wifi (which have the captive portal) by setting the configuration, NEHotspotHelperCommand presentUi command never calls. Please check my below code

 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")
        }

    })

I have one more issue in this when there is mobile network available and I try to check the captive portal is always return me the success. Please check the below code as well

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()

}

Solution

  • NEHotspotHelperCommandType.presentUI will be called for HotspotHelper who have responded first for NEHotspotHelperCommandType.evaluate command, so what we have done is remove most of the code from evaluation command and responded as fast as possible. Now its working fine for us.