简体   繁体   English

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

[英]Can we test Face ID in simulator?

Can we test biometric authentication using the simulator?我们可以使用模拟器测试生物认证吗?

The iPhone X Simulator shows a menu for Face ID enrollment, but after enabling that, what can I do? iPhone X Simulator 显示了 Face ID 注册菜单,但启用该菜单后,我该怎么办?

How it will recognize a face for authentication?它将如何识别人脸进行身份验证?

iPhone X 模拟器 - 面容 ID 设置

Simulator does not recognise a face but allows you to simulate a matching and non-matching faces, if you've enabled Enrolled option from Face ID . Simulator 无法识别人脸,但允许您模拟匹配和不匹配的人脸, Enrolled是您已从Face ID启用Enrolled选项。


Add following code to your view controller and try with 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 authentication will prompt you for first time to allow FaceID detection for your app. FaceID 身份验证将首次提示您允许对您的应用进行 FaceID 检测。

在此处输入图片说明


Now enable Face ID enrolment and run your app to test Face ID simulation Testing.现在启用 Face ID 注册并运行您的应用程序以测试 Face ID 模拟测试。

Here is simulation result for matching and non-matching faces.这是匹配和非匹配人脸的模拟结果。

Result for matching face:人脸匹配结果:

在此处输入图片说明


Result for non-matching face:不匹配人脸的结果:

在此处输入图片说明


The simulator just simulates the outcome of a correct and a failed face recognition, just like it does with Touch ID.模拟器只是模拟正确和失败的人脸识别结果,就像使用 Touch ID 一样。 It does not recognize faces .不识别面孔

As you asking but after enabling that, what can I do?正如你所问,但启用后,我能做什么?

Just like a touch Id enrolment, You can verify things with face-Id on iPhone-X.就像触摸 ID 注册一样,您可以在 iPhone-X 上使用 face-Id 验证事物。 However simulator have some limitations like Appstore etc. With face-Id enrolment you can do following things -但是模拟器有一些限制,如 Appstore 等。通过 face-Id 注册,您可以执行以下操作 -

  • Use Face ID to make purchases.使用面容 ID 进行购买。
  • Sign in with Face ID (Sign in to apps).使用面容 ID 登录(登录应用)。
  • Autofill passwords in Safari.在 Safari 中自动填充密码。
  • In the iTunes Store, App Store, and iBooks Store.在 iTunes Store、App Store 和 iBooks Store 中。

See more at Apple在 Apple 查看更多

same as give by @krunal just 2nd if should be outside of 1st.与@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
     }
 }
}

See this article.看到这篇文章。 You can create the Biometrics.m, Biometrics.h, and bridging-header.h files within you UITests folder and update your UI test target to use that bridging header.您可以在 UITests 文件夹中创建 Biometrics.m、Biometrics.h 和 bridging-header.h 文件,并更新您的 UI 测试目标以使用该桥接头。 https://github.com/KaneCheshire/BiometricAutomationDemo https://github.com/KaneCheshire/BiometricAutomationDemo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM