我们可以在模拟器中测试Face ID吗?

60

我们能使用模拟器测试生物识别身份验证吗?

iPhone X模拟器显示了面部ID注册菜单,但启用后我能做什么?

它如何识别面部进行身份验证?

iPhone X模拟器 - 面部ID设置


1
在后续版本中,您可以在模拟器的“功能”选项下找到“Face ID”。 - maxmitz
5个回答

56

如果您已启用Face ID选项,则模拟器无法识别面孔,但允许您模拟匹配和不匹配的面孔。


将以下代码添加到您的视图控制器中,并尝试使用 Face ID。

import LocalAuthentication

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        localAuthentication()
    }



    func localAuthentication() -> Void {

        let laContext = LAContext()
        var error: NSError?
        let biometricsPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics

        if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {

            if let laError = error {
                print("laError - \(laError)")
                return
            }

            var localizedReason = "Unlock device"
            if #available(iOS 11.0, *) {
                if (laContext.biometryType == LABiometryType.faceID) {
                    localizedReason = "Unlock using Face ID"
                    print("FaceId support")
                } else if (laContext.biometryType == LABiometryType.touchID) {
                    localizedReason = "Unlock using Touch ID"
                    print("TouchId support")
                } else {
                    print("No Biometric support")
                }
            } else {
                // Fallback on earlier versions
            }


            laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in

                DispatchQueue.main.async(execute: {

                    if let laError = error {
                        print("laError - \(laError)")
                    } else {
                        if isSuccess {
                            print("sucess")
                        } else {
                            print("failure")
                        }
                    }

                })
            })
        }


    }
}

首次使用FaceID身份验证时,将提示您允许应用程序检测FaceID。

enter image description here


现在启用Face ID注册,并运行您的应用程序以测试Face ID模拟测试。

以下是匹配和不匹配面孔的模拟结果。

匹配面孔的结果:

enter image description here


不匹配面孔的结果:

enter image description here



1
有没有办法自定义那个正方形的 Face ID 提示框?我想在那里添加一些文本。这可行吗? - Andrey Solovyov
1
@AndreySolovyov 它不是可定制的。 - Tamás Sengel

9

模拟器仅仅模拟正确和失败的人脸识别结果,就像它对Touch ID一样。它并不会识别人脸


谢谢,我明白了。这是我第一次使用生物识别技术。因此,在认证过程中我们必须按下匹配人脸才能成功进行身份验证。 - technerd

1

0

和 @krunal 给出的一样,只是第二个 if 应该在第一个之外。

import LocalAuthentication

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    localAuthentication()
}

func localAuthentication() -> Void {

    let laContext = LAContext()
    var error: NSError?
    let biometricsPolicy = LAPolicy.deviceOwnerAuthenticationWithBiometrics

    if (laContext.canEvaluatePolicy(biometricsPolicy, error: &error)) {



        var localizedReason = "Unlock device"
        if #available(iOS 11.0, *) {
            if (laContext.biometryType == LABiometryType.faceID) {
                localizedReason = "Unlock using Face ID"
                print("FaceId support")
            } else if (laContext.biometryType == LABiometryType.touchID) {
                localizedReason = "Unlock using Touch ID"
                print("TouchId support")
            } else {
                print("No Biometric support")
            }
        } else {
            // Fallback on earlier versions
        }


        laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in

            DispatchQueue.main.async(execute: {

                if let laError = error {
                    print("laError - \(laError)")
                } else {
                    if isSuccess {
                        print("sucess")
                    } else {
                        print("failure")
                    }
                }
            })
        })
    }
//This should be outside of if

 if let laError = error {
        print("laError - \(laError)")
        return
     }
 }
}

0

你问了 - 但是启用后,我能做什么?

就像Touch ID注册一样,您可以在iPhone X上使用Face ID进行验证。 然而,模拟器有一些限制,如Appstore等。 使用Face ID注册,您可以执行以下操作 -

  • 使用Face ID进行购买。
  • 使用Face ID登录(登录应用程序)。
  • 在Safari中自动填充密码。
  • 在iTunes Store,App Store和iBooks Store中。

在Apple查看更多信息


感谢您的输入,我一定会检查链接以获取更多详细信息。 - technerd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接