Skip to content

Instantly share code, notes, and snippets.

@4np
Last active December 20, 2016 15:01
Show Gist options
  • Save 4np/c1e3f45206f96e5552ecbf0cc59ebffd to your computer and use it in GitHub Desktop.
Save 4np/c1e3f45206f96e5552ecbf0cc59ebffd to your computer and use it in GitHub Desktop.
Using touch ID
import LocalAuthentication
...
private func authenticateUser() {
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 {
debugPrint("authorized!")
} 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment