首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NEHotspotHelper.register未收到回叫iOS11

NEHotspotHelper.register未收到回叫iOS11
EN

Stack Overflow用户
提问于 2017-10-16 09:35:34
回答 1查看 5K关注 0票数 4

我正在NEHotspotHelper上工作,试图注册,但没有收到回电。首先,

我启用的功能:网络扩展

然后添加以下代码,

代码语言:javascript
运行
复制
 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
        }
    }

如果我错过了任何一步,请告诉我。

EN

回答 1

Stack Overflow用户

发布于 2018-09-24 04:38:20

您应该检查register()函数的结果。如果它返回的是false,那么某些东西可能配置得不正确。请参阅下面配置说明的完整列表。

另外,在您提供的屏幕截图中,您为Hotspot配置启用了权限,但是您调用的API是针对Hotspot Helper的。这两项功能要求的权利非常不同。您需要确保为Hotspot Helper配置了所有东西,以调用该API。同样,有关详细信息,请参阅下面。有关这些类似名称的API的差异的更多细节,请参见Hotspot Helper与Hotspot配置

要使用NEHotspotHelper:

  1. 申请网络扩展权限. 这需要在苹果的网站在这里完成。
  2. 修改您的配置文件.http://developer.apple.com。点击Edit靠近你的个人资料。在上面写着Entitlements的底部,选择一个包含网络扩展权限的。
  3. 更新应用程序的权益文件. 应用程序必须将com.apple.developer.networking.HotspotHelper设置为其应享权利之一。应享权利的值是布尔值设置为true。
  4. 添加背景模式 应用程序的Info.plist必须包括一个包含network-authenticationUIBackgroundModes数组。

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

  1. NEHotspotHelper.register() 调用函数. 此方法应在应用程序启动时调用一次。再次调用它将不会产生任何效果,并导致返回false。

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

  1. 理解何时调用此回调。 从文档中,还不完全清楚何时才会调用此回调。例如,人们可能假设可以使用NEHotspotHelper来监视网络连接。然而,回调将(仅?)当用户导航到Settings应用程序并转到Wi页面时调用。 因为只有在设置应用程序中的用户调用您的回调时,您才应该附加到调试器上并使用print()

Swift实例

代码语言:javascript
运行
复制
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)

资料来源:

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46767138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档