iOS 11 模拟器不允许 LAContext 和 FaceID

iOS 11 Simulator not allowing LAContext and FaceID

我已经 运行 最新的 Xcode 9 GM(2017 年 9 月 13 日)并在模拟器中设置了 Hardware > Face ID > Enrolled 以及 Deployment Target 11.0。但是我收到错误代码 -6 LAErrorTouchIDNotAvailable

我是否缺少某些设置?

let myContext = LAContext()
let myLocalizedReasonString = "You are pretty"

var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
    if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
        myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in
            if success {

                print("// User authenticated successfully, take appropriate action")
            } else {
                 print(" // User did not authenticate successfully, look at error and take appropriate action")
            }
        }
    } else {
         print(" // Could not evaluate policy; look at authError and present an appropriate message to user")
    }
} else {
     print(" // Fallback on earlier versions")
}

我认为 iphone X 模拟器的 faceID 目前无法使用,希望他们能尽快修复它...

https://forums.developer.apple.com/thread/86779

我们可以做一个错误报告,看看它是否加快了速度:P https://developer.apple.com/bug-reporting

由于框架错误,Face ID 在 Xcode 9 GM 中不起作用。 Xcode 9.1 修复了这个问题。

您可以在 iPhone 8 模拟器中测试您的应用,并确保它与 Touch ID 或 运行 Xcode 9.1 测试版一起正常工作,并在那里测试 Face ID 支持。

XCode今天9.1测试版出来了,原代码在模拟器上应该可以完美运行!

根据 LAContext 的 Apple 文档,我们需要添加带有使用原因字符串的密钥 NSFaceIDUsageDescription,因为这将显示在设备上使用 FaceId 的授权请求。

示例将此添加到 info.plist:

NSFaceIDUsageDescription

将其设置为字符串类型,并在访问 Face ID 摄像头的提示请求中添加您想要显示的文本。

"Your app" request your permission to use Face ID, for you to login to your account / unlock your notes / what ever reason in the end.

通过添加这个,你可以进入 iPhone X 的模拟器,系统会提示你输入 Face ID,按接受,一切都应该完美。

记得为模拟器注册生物识别支持,方法是进入 Simulator -> Hardware -> Face ID / Touch ID -> Enrolled

然后你只需要按下Match / Non-Matching Touch / Face ID,来测试你的处理能力

有关更多详细信息并查看 Apple 的文档:https://developer.apple.com/documentation/localauthentication/lacontext

---- 编辑----

这对我在 Xcode 9.0 和 9.1

都有效

Face ID 现在可以使用 Xcode 9.1。按照以下步骤在模拟器中对其进行测试。

在目标的 info.plist 文件中添加隐私声明。

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


现在启用面容 ID 注册和 运行 您的应用程序以测试面容 ID 模拟测试。

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

人脸匹配结果:


不匹配人脸的结果: