gpt4 book ai didi

swift - NEHotspotHelper.register 未收到回调 iOS11

转载 作者:可可西里 更新时间:2023-11-01 00:56:23 30 4
gpt4 key购买 nike

我正在使用 NEHotspotHelper 并尝试注册但没有收到回电。首先,

I enabled Capability : Network Extensions enter image description here

然后添加以下代码,

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

如果我遗漏了任何步骤,请告诉我。

最佳答案

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

此外,在您提供的屏幕截图中,您已为热点配置 启用了权利,但您调用的 API 是针对热点助手 的。这两个功能需要非常不同的权利。您需要确保为 Hotspot Helper 配置所有内容以调用该 API。同样,请参阅下面的完整详细信息。有关这些名称相似的 API 的区别的更多详细信息,请参阅 Hotspot Helper vs. Hotspot Configuration


使用 NEHotspotHelper:

  1. 申请网络扩展授权。

    这需要在 Apple's website here 完成。

  2. 修改您的配置文件。

    转到 http://developer.apple.com 。点击您个人资料附近的编辑。在显示 Entitlements 的底部,选择包含 Network Extension entitlement 的那个。

  3. 更新您应用的授权文件。

    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.

  4. 添加背景模式

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

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

  5. 调用NEHotspotHelper.register()函数。

    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。否则,上述步骤之一可能未正确配置。

  6. 了解何时调用此回调。

    从文档中,并不完全清楚调用此回调的确切时间。例如,人们可能会假设 NEHotspotHelper 可用于监视网络连接。但是,回调将(仅?)在用户导航到“设置”应用并转到 Wi-Fi 页面时调用。

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

Swift 示例

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)

来源:

关于swift - NEHotspotHelper.register 未收到回调 iOS11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46767138/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com