Skip to content

Instantly share code, notes, and snippets.

@andresr-dev
Last active February 20, 2023 21:47
Show Gist options
  • Save andresr-dev/ed1e4e290ebfafe80ddb0fbd3731e4a0 to your computer and use it in GitHub Desktop.
Save andresr-dev/ed1e4e290ebfafe80ddb0fbd3731e4a0 to your computer and use it in GitHub Desktop.
This is how to authenticate a user with biometrics in swift
import LocalAuthentication
class AuthenticationService {
// We need to provide a FaceID description in info.plist:
// Privacy - Face ID Usage Description -> Identify yourself!
func authenticate() {
let context = LAContext()
var error: NSError?
// In objc an inout parameter is known as Pointer that's why &error
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
// Shown just for users with touch ID
let reason = "Identify yourself!"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { [weak self] success, authenticationError in
DispatchQueue.main.async {
if success {
// Authentication succeeded
} else {
// Authentication failed
}
}
}
} else {
// Device Biometrics unavailable
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment