如何使用touchIDAuthenticationAllowableReuseDuration



当应用程序启动或将进入前台时,我正在通过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: {
}) {
}

相关内容

  • 没有找到相关文章

最新更新