Among these answers, I got stuck every time I ran an authentication code.
Make sure you import LocalAuthentication to use LAContext and verify privacy
With Swift 5, this is what worked for me.
import UIKit import LocalAuthentication class ViewController: UIViewController { @IBOutlet weak var lbl: UILabel! override func viewDidLoad() { super.viewDidLoad() } @IBAction func checkUserAuthentication(_ sender: Any) { let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { let reason = "Identify yourself!" context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { [unowned self] success, authenticationError in DispatchQueue.main.async { if success { //Authentication Success self.lbl.text = "Great!! you are authorised" } else { let ac = UIAlertController(title: "Authentication failed", message: "Sorry!", preferredStyle: .alert) ac.addAction(UIAlertAction(title: "OK", style: .default)) self.present(ac, animated: true) } } } } else { let ac = UIAlertController(title: "Touch ID not available", message: "Your device is not configured for Touch ID.", preferredStyle: .alert) ac.addAction(UIAlertAction(title: "OK", style: .default)) present(ac, animated: true) } } }
source share