未收到NEHotspotHelper.register回调iOS11 [英] NEHotspotHelper.register not received call back iOS11

查看:317
本文介绍了未收到NEHotspotHelper.register回调iOS11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NEHotspotHelper,并尝试注册但未收到回电.首先,

I am working on NEHotspotHelper and trying to register but not receiving call back. Firstly,

我启用了功能:网络扩展

I enabled Capability : Network Extensions

然后添加以下代码,

 let options: [String: NSObject] = [kNEHotspotHelperOptionDisplayName : "ABC" as NSObject]
    let queue: DispatchQueue = DispatchQueue(label: "com.ABC", attributes: DispatchQueue.Attributes.concurrent)

    NSLog("Started wifi scanning.")

    NEHotspotHelper.register(options: options, queue: queue) { (cmd: NEHotspotHelperCommand) in
        NSLog("Received command: \(cmd.commandType.rawValue)")

        if cmd.commandType == NEHotspotHelperCommandType.filterScanList {
            //Get all available hotspots
            let list: [NEHotspotNetwork] = cmd.networkList!
            //Figure out the hotspot you wish to connect to

            print(list)

        } else if cmd.commandType == NEHotspotHelperCommandType.evaluate {
            if let network = cmd.network {
                //Set high confidence for the network
                network.setConfidence(NEHotspotHelperConfidence.high)

                let response = cmd.createResponse(NEHotspotHelperResult.success)
                response.setNetwork(network)
                response.deliver() //Respond back
            }
        } else if cmd.commandType == NEHotspotHelperCommandType.authenticate {
            //Perform custom authentication and respond back with success
            // if all is OK
            let response = cmd.createResponse(NEHotspotHelperResult.success)
            response.deliver() //Respond back
        }
    }

请让我知道是否缺少任何步骤.

Kindly let me know if I am missing any step.

推荐答案

您应检查register()函数的结果.如果返回false,则可能未正确配置某些内容.请参阅下面的配置说明的完整列表.

You should check the result of the register() function. If it's returning false, something is probably not configured correctly. See the full list of configuration instructions below.

在提供的屏幕截图中,您还启用了热点配置的权利,但是您要调用的API是针对热点帮助器的.这两个功能要求的权利大不相同.您需要确保为Hotspot Helper进行了所有配置,以调用该API.同样,请参阅下面的完整详细信息.有关这些相似命名的API的区别的更多详细信息,请参见热点帮助与热点配置.

Also in the screenshot you provided, you have the entitlements enabled for Hotspot Configuration, but the API you're calling is for Hotspot Helper. The two features require very different entitlements. You'll need to make sure everything is configured for Hotspot Helper to call that API. Again, see below for full details. See Hotspot Helper vs. Hotspot Configuration for more details about the differences of these similarly named APIs.

  1. 申请网络扩展权利.

这需要在 Apple的网站上完成.

修改您的配置文件.

转到 http://developer.apple.com .在您的个人资料旁点击Edit.在显示Entitlements的底部,选择一个包含Network Extension权利的内容.

Go to http://developer.apple.com. Hit Edit near your profile. On the bottom where it says Entitlements, choose the one that contains the Network Extension entitlement.

更新您的应用的权利文件.

应用程序必须将com.apple.developer.networking.HotspotHelper设置为其权利之一.权利的值是设置为true的布尔值.

The application must set com.apple.developer.networking.HotspotHelper as one of its entitlements. The value of the entitlement is a boolean set to true.

  • 添加后台模式

    应用程序的Info.plist必须包含包含network-authenticationUIBackgroundModes数组.

    The application's Info.plist must include a UIBackgroundModes array containing network-authentication.

    请注意,与所有其他背景模式都转换为人类可读的字符串不同,该模式将保留为network-authentication.

    Note that unlike all the other background modes that are converted to human readable strings, this one will stay as network-authentication.

    调用NEHotspotHelper.register()函数.

    Call the NEHotspotHelper.register() function.

    该方法在应用程序启动时应被调用一次.再次调用将无效,并导致返回错误.

    This method should be called once when the application starts up. Invoking it again will have no effect and result in false being returned.

    您应确保该函数返回true.否则,上述步骤之一可能配置不正确.

    You should make sure the function returns true. Otherwise something one of the above steps is probably not configured properly.

    了解何时调用此回调.

    从文档中,尚不清楚何时确切调用此回调.例如,可能假设NEHotspotHelper可用于监视网络连接.但是,当用户导航到设置"应用并转到Wi-Fi页面时,将仅调用该回调.

    From the documentation, it's not entirely clear when exactly this callback will be called. For example, one might assume that NEHotspotHelper could be used to monitor for network connections. However, the callback will (only?) be called when the user navigates to the Settings app and goes to the Wi-Fi page.

    由于只有在设置"应用中的用户时才会调用回调,因此您应该附加到调试器并使用print().

    Since your callback will be called only while the user in the Settings app, you should attach to the debugger and use print().

    快速示例

    let targetSsid = "SFO WiFi"
    let targetPassword = "12345678"
    let targetAnnotation: String = "Acme Wireless"
    
    let options: [String: NSObject] = [
      kNEHotspotHelperOptionDisplayName: targetAnnotation as NSString
    ]
    
    let queue = DispatchQueue(label: "com.example.test")
    
    let isAvailable = NEHotspotHelper.register(options: options, queue: queue) { (command) in
      switch command.commandType {
      case .evaluate,
           .filterScanList:
        let originalNetworklist = command.networkList ?? []
        let networkList = originalNetworklist.compactMap { network -> NEHotspotNetwork? in
          print("networkName: \(network.ssid); strength: \(network.signalStrength)")
          if network.ssid == targetSsid {
            network.setConfidence(.high)
            network.setPassword(targetPassword)
            return network
          }
          return nil
        }
        let response = command.createResponse(.success)
        response.setNetworkList(networkList)
        response.deliver()
      default:
        break
      }
    }
    
    assert(isAvailable)
    

    来源:

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆