gpt4 book ai didi

ios - 如何使用 touchIDAuthenticationAllowableReuseDuration

转载 作者:行者123 更新时间:2023-11-28 07:34:23 26 4
gpt4 key购买 nike

我在应用程序启动或进入前台时通过 LAContext 对用户进行身份验证。如果设备被锁定,则将要求用户两次授权自己。为避免这种行为,我将 context.touchIDAuthenticationAllowableReuseDuration 值设置为 240,但它没有按预期工作。用户还是要给自己授权两次。我做错了什么?


import LocalAuthentication

class AccessControl {

internal var context = LAContext()
private var policy: LAPolicy = .deviceOwnerAuthentication
private var reason: String = NSLocalizedString("auhenticationLocalizedFallbackTitle", comment: "")

init() {
context.touchIDAuthenticationAllowableReuseDuration = 240
}

func evaluateUserWithBiometricsOrPasscode(success: @escaping () -> Void, error: @escaping () -> Void) {

guard context.canEvaluatePolicy(policy, error: nil) else {
error()
return
}
context.evaluatePolicy(policy, localizedReason: reason) { eStatus, eError in
DispatchQueue.main.async {
if eStatus {
success()
} else {
error()
}
}
}
}

}

最佳答案

您需要每次都使用相同的 LAContext 对象才能获得该行为。

class AccessControl {

// MARK: - Singleton
public static let shared = AccessControl()

// Policy
private var policy: LAPolicy = .deviceOwnerAuthentication

// Reason
private var reason: String = NSLocalizedString("auhenticationLocalizedFallbackTitle", comment: "")

// Context
lazy var context: LAContext = {
let mainContext = LAContext()
if #available(iOS 9.0, *) {
// specify your interval
mainContext.touchIDAuthenticationAllowableReuseDuration = 60
}
return mainContext
}()


// Evaluate
func evaluateUserWithBiometricsOrPasscode(success: @escaping () -> Void, error: @escaping () -> Void) {

guard context.canEvaluatePolicy(policy, error: nil) else {
error()
return
}
context.evaluatePolicy(policy, localizedReason: reason) { eStatus, eError in
DispatchQueue.main.async {
if eStatus {
success()
} else {
error()
}
}
}
}
}

并像下面这样调用此函数:这也适用于FaceID 身份验证

AccessControl.shared.evaluateUserWithBiometricsOrPasscode(success: {

}) {

}

关于ios - 如何使用 touchIDAuthenticationAllowableReuseDuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53630614/

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