【问题标题】:Can we test Face ID in simulator?我们可以在模拟器中测试 Face ID 吗?
【发布时间】:2018-04-19 22:07:24
【问题描述】:

我们可以使用模拟器测试生物特征认证吗?

iPhone X 模拟器显示了一个面容 ID 注册菜单,但启用后,我该怎么办?

它将如何识别人脸进行身份验证?

【问题讨论】:

    标签: ios simulator biometrics iphone-x face-id


    【解决方案1】:

    模拟器无法识别人脸,但允许您模拟匹配和不匹配的人脸,前提是您已启用来自Face IDEnrolled 选项。


    将以下代码添加到您的视图控制器并尝试使用 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 检测。


    现在启用 Face ID 注册并运行您的应用来测试 Face ID 模拟测试。

    这是匹配和不匹配人脸的模拟结果。

    人脸匹配结果:


    人脸不匹配的结果:


    【讨论】:

    • 能否以某种方式自定义方形人脸 ID 提示?我想在那里添加一些文字。有可能吗?
    • @AndreySolovyov 这不是可定制的。
    【解决方案2】:

    模拟器只是模拟人脸识别正确和失败的结果,就像使用 Touch ID 一样。它无法识别面孔

    【讨论】:

    • 谢谢,明白了。我第一次使用生物识别技术。所以我们必须在认证过程中按下match face才能认证成功。
    【解决方案3】:

    正如你所问但启用后,我能做什么?

    就像一个 touch Id 注册一样,您可以在 iPhone-X 上使用 face-Id 验证事物。 但是模拟器有一些限制,比如 Appstore 等。 通过 face-Id 注册,您可以执行以下操作 -

    • 使用面容 ID 购物。
    • 使用面容 ID 登录(登录应用)。
    • 在 Safari 中自动填充密码。
    • 在 iTunes Store、App Store 和 iBooks Store 中。

    See more at Apple

    【讨论】:

    • 感谢您的意见,我一定会检查链接以获取更多详细信息。
    【解决方案4】:

    与@krunal 给出的相同,如果应该在第一个之外,则只是第二个。

    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
         }
     }
    }
    

    【讨论】:

      【解决方案5】:

      请参阅这篇文章。您可以在 UITests 文件夹中创建 Biometrics.m、Biometrics.h 和 bridging-header.h 文件,并更新您的 UI 测试目标以使用该桥接头。 https://github.com/KaneCheshire/BiometricAutomationDemo

      【讨论】:

        最近更新 更多