6

我已经运行了最新的 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")
}
4

5 回答 5

7

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

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

于 2017-09-14T19:29:26.043 回答
5

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

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

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

于 2017-09-13T07:55:58.097 回答
4

Face ID is working now, with Xcode 9.1. Follow these steps to test it in Simulator.

Add privacy statement in your target's info.plist file.

enter image description here

Import LocalAuthentication framework to your project and add following code to your view controller and try with 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.

enter image description here


Now enable Face ID enrolment and run your app to test Face ID simulation Testing.

enter image description here

Here is simulation result for matching and non-matching faces.

Result for matching face:

enter image description here


Result for non-matching face:

enter image description here

于 2017-11-07T16:50:54.173 回答
1

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

示例将其添加到 info.plist:

NSFaceIDUsageDescription

将其设置为 String 类型,并在访问 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 中都适用于我

于 2017-11-04T07:56:09.890 回答
1

XCode 9.1 beta 今天发布了,其中原始代码应该可以在模拟器中完美运行!

于 2017-09-27T19:06:04.563 回答