Last active
February 20, 2023 21:47
-
-
Save andresr-dev/ed1e4e290ebfafe80ddb0fbd3731e4a0 to your computer and use it in GitHub Desktop.
This is how to authenticate a user with biometrics in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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