«`swift
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
// MARK: — Outlets
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var unlockButton: UIButton!
// MARK: — Properties
private let authenticationContext = LAContext()
private var isAuthenticated = false
// MARK: — Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Check if biometrics are available
if authenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) {
unlockButton.setTitle(«Unlock with Biometrics», for: .normal)
} else {
unlockButton.setTitle(«Unlock with Password», for: .normal)
}
}
// MARK: — Actions
@IBAction func unlockButtonTapped(_ sender: UIButton) {
// Check if biometrics are available
if authenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) {
authenticateWithBiometrics()
} else {
authenticateWithPassword()
}
}
// MARK: — Private Methods
private func authenticateWithBiometrics() {
authenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: «Unlock the app with biometrics») { [weak self] success, error in
guard let self = self else { return }
if success {
self.isAuthenticated = true
self.performSegue(withIdentifier: «main», sender: nil)
} else {
let errorMessage = error?.localizedDescription ?? «Authentication failed»
self.showAlert(title: «Error», message: errorMessage)
}
}
}
private func authenticateWithPassword() {
let password = passwordTextField.text
if password == «password» {
isAuthenticated = true
performSegue(withIdentifier: «main», sender: nil)
} else {
showAlert(title: «Error», message: «Incorrect password»)
}
}
private func showAlert(title: String, message: String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: «OK», style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
«`