如何测试适用于 macOS 的 Touch ID?

How can you test Touch ID for macOS?

我正在为 macOS 开发一个应用程序。我们想在新的 MacBook Pro 中使用 Touch ID 传感器来解锁我们的应用程序。在 iOS,您可以从模拟器测试本地身份验证框架。

有没有办法为 Mac 应用测试 Touch ID?最新的 XCode 版本提供了一个不错的 Touch Bar 模拟器,但我找不到 Touch ID 的任何东西。

谢谢!

Rick Fillion(来自 1Password)非常友好地提供了一些建议:https://twitter.com/rickfillion/status/794370861646172160

使用LAPolicy.DeviceOwnerAuthentication进行测试。

这是我的代码 运行 (Swift 2.3):

import Cocoa
import LocalAuthentication

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let myContext = LAContext()
        let myLocalizedReasonString = "unlock itself"

        var authError: NSError? = nil
        if #available(iOS 8.0, OSX 10.12, *) {
            if myContext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthentication, error: &authError) {
                myContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthentication, localizedReason: myLocalizedReasonString) { (success, evaluateError) in
                    if (success) {
                        // User authenticated successfully, take appropriate action
                        print("Success")
                    } else {
                        // User did not authenticate successfully, look at error and take appropriate action
                        print("Failure")
                    }
                }
            } else {
                // Could not evaluate policy; look at authError and present an appropriate message to user
                print("Evaluation")
                print(authError)
            }
        } else {
            // Fallback on earlier versions
            print("Fallback")
        }
        // Do any additional setup after loading the view.
    }
}