是否可以在用户看到 UIView 之前询问用户的 PIN、Face ID 或 Touch ID?

Is it possible to ask for the user's PIN, Face ID or Touch ID before he sees a UIView?

我想为 phone 所有者以外的所有人锁定应用程序的历史记录部分。我不喜欢强迫用户只为这个应用创建一个帐户或创建一个新的 PIN。我可以使用他已经设置的 PIN、Face ID 或 Touch ID 进行授权吗?

Local Authentication 框架将处理此问题。

这是苹果的一部分code sample:

/// Logs out or attempts to log in when the user taps the button.
@IBAction func tapButton(_ sender: UIButton) {

    if state == .loggedin {

        // Log out immediately.
        state = .loggedout

    } else {

        // Get a fresh context for each login. If you use the same context on multiple attempts
        //  (by commenting out the next line), then a previously successful authentication
        //  causes the next policy evaluation to succeed without testing biometry again.
        //  That's usually not what you want.
        context = LAContext()

        context.localizedCancelTitle = "Enter Username/Password"

        // First check if we have the needed hardware support.
        var error: NSError?
        if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {

            let reason = "Log in to your account"
            context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in

                if success {

                    // Move to the main thread because a state update triggers UI changes.
                    DispatchQueue.main.async { [unowned self] in
                        self.state = .loggedin
                    }

                } else {
                    print(error?.localizedDescription ?? "Failed to authenticate")

                    // Fall back to a asking for username and password.
                    // ...
                }
            }
        } else {
            print(error?.localizedDescription ?? "Can't evaluate policy")

            // Fall back to a asking for username and password.
            // ...
        }
    }
}