如何使用 Touch ID 授权后才执行 Segue

How to perform the Segue only after authorization with Touch ID

也许有人可以提供帮助,只是想使用 Segue 登录按钮转移到第二个 ViewController 只有在使用 Touch ID 授权后 但是应用程序在用户按下后仍在执行 Segue按钮.

import UIKit
import LocalAuthentication

class LoginWindowViewController: UIViewController {


    @IBAction func loginButton(_ sender: Any) {

        let context: LAContext = LAContext()
        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil){

            context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "For login you need to use your TouchID", reply: {(wasSuccessful, error) in if wasSuccessful {
                DispatchQueue.main.async {
   self.shouldPerformSegue(withIdentifier: "LoginComplete", sender: self.navigationController)
                }
            }else{
              print ("Bad TouchID")
                }


            })

        }


    }

谢谢

尝试实现此函数并从调用它的位置进行检查:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {

}

请记住,当您从代码中调用 segue 时,如果您已经将一个 segue 操作从您的按钮添加到情节提要中的下一个 ViewController,您应该删除它并且只在它们之间创建连接你的 LoginViewController 到你想呼叫的人

documentation 所述,如果您不重写 shouldPerformSegue 方法,则所有 segues 的默认实现 returns true

shouldPerformSegue(withIdentifier:sender:) 方法应根据您的实施的 return 值确定是否调用具有指定标识符的 segue。

因为你已经检查了TouchID授权是否成功,所以你实际上不需要调用shouldPerformSegue,而是需要调用performSegue.

context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "For login you need to use your TouchID", reply: {(wasSuccessful, error) in
    if wasSuccessful {
        DispatchQueue.main.async {
            self.performSegue(withIdentifier: "LoginComplete", sender: self.navigationController)
        }
    }else{
        print ("Bad TouchID")
    }
})