gpt4 book ai didi

ios - 如何在我的应用程序中使用密码锁定场景?

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:00 25 4
gpt4 key购买 nike

实际上,我构建了一个包含本地身份验证的应用。

到目前为止我的代码:

func authenticateUser() {
let authenticationContext = LAContext()
var error: NSError?
let reasonString = "Touch the Touch ID sensor to unlock."

// Check if the device can evaluate the policy.
if authenticationContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {

authenticationContext.evaluatePolicy( .deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success, evalPolicyError) in

if success {
print("success")
} else {
if let evaluateError = error as NSError? {
// enter password using system UI
}

}
})

} else {
print("toch id not available")
// enter password using system UI
}
}

我的问题是当应用程序没有触摸 ID 或无效指纹时,我想使用密码锁定场景。

如下图:

enter image description here

我该怎么做?

最佳答案

您应该使用 .deviceOwnerAuthentication 而不是 .deviceOwnerAuthenticationWithBiometrics 来评估策略。使用此参数,系统将使用生物识别身份验证(如果可用),否则它会显示密码屏幕。如果生物识别身份验证可用但失败,则后备按钮会重定向到密码屏幕。参见 documentation :

If Touch ID or Face ID is available, enrolled, and not disabled, the user is asked for that first. Otherwise, they are asked to enter the device passcode.

Tapping the fallback button switches the authentication method to ask the user for the device passcode.

所以你的代码将是:

func authenticateUser() {
let authenticationContext = LAContext()
var error: NSError?
let reasonString = "Touch the Touch ID sensor to unlock."

// Check if the device can evaluate the policy.
if authenticationContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthentication, error: &error) {

authenticationContext.evaluatePolicy( .deviceOwnerAuthentication, localizedReason: reasonString, reply: { (success, evalPolicyError) in

if success {
print("success")
} else {
// Handle evaluation failure or cancel
}
})

} else {
print("passcode not set")
}
}

关于ios - 如何在我的应用程序中使用密码锁定场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44941863/

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