【问题标题】:Touch Id and Face Id authenticationTouch ID 和 Face ID 身份验证
【发布时间】:2023-01-26 22:01:40
【问题描述】:

如果我在我的应用程序上实施生物认证

是否有必要在设备上注册生物识别技术? 有没有可能是我的设备没有注册 touch 或 face Id? 如果“是”,我如何从我的应用程序启用和注册面部和触摸 ID?

任何建议或帮助都是合适的,在此先感谢。

我试图在我的应用程序中实施生物识别身份验证。困难在于,当用户应用程序未注册或未启用人脸和触摸 ID 时,我如何从我的应用程序启用和注册生物识别?

【问题讨论】:

    标签: java android swift biometrics


    【解决方案1】:

    当我们被告知 Touch ID/Face ID 是否成功时,它可能不在主线程上。这意味着我们需要使用 async() 来确保我们在主线程上执行任何用户界面代码。

    任务 1 的工作由 LAContext 类的 canEvaluatePolicy() 方法完成,请求安全策略类型 .deviceOwnerAuthenticationWithBiometrics。任务 2 的工作由同一个类的 evaluatePolicy() 完成,使用相同的策略类型,但它接受一个尾随闭包,告诉我们策略评估的结果:是否成功,如果不成功,原因是什么?

    正如我所说,所有这些都是由本地身份验证框架提供的,因此我们需要做的第一件事就是导入该框架。在导入 UIKit 上面添加:

    import LocalAuthentication
    

    现在这是 authenticateTapped() 方法的新代码。我们已经了解了它的作用,所以这不足为奇:

    @IBAction func authenticateTapped(_ sender: Any) {
    let context = LAContext()
    var error: NSError?
    
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        let reason = "Identify yourself!"
    
        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
            [weak self] success, authenticationError in
    
            DispatchQueue.main.async {
                if success {
                    self?.unlockSecretMessage()
                } else {
                    // error
                }
            }
        }
    } else {
        // no biometry
    }
    

    }

    要捕获身份验证失败错误,请将 // 错误注释替换为:

    let ac = UIAlertController(title: "Authentication failed", message: "You could not be verified; please try again.", preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "OK", style: .default))
    self.present(ac, animated: true)
    

    如果生物识别不可用,我们还需要显示错误,因此将 // no Touch ID 注释替换为:

    let ac = UIAlertController(title: "Biometry unavailable", message: "Your device is not configured for biometric authentication.", preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "OK", style: .default))
    self.present(ac, animated: true)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多