¡Hola mundo!
In the project I work we have biometric authentication and this has been a huge mistery for me. But the time has come! I decided I wanted to learn a little bit more about it. How does it work and how it is managed.
This is a super simple study repo to put on practice biometrics authentication in Swift.
- A login button that must call biometrics
- If the authentication is successful the user must enter the app, and maybe find The Secret of Monkey Island ™
- If the authentication fails we must ask the user their device PIN
- XCode
- Swift
- SwiftUI
There are two simple views, one with a button to login. This button is the one that will request the biometrics. The LoginView
will only be rendered if the user is not already logged in. This is managed with a Published variable in our AppState called loggedIn
.
When this variable value changes to true we show the ContentView that is - or not - The Secret of Monkey Island ™. In the real life this would be a real app.
Apple documentation is very good regarding this. I've created an AppState
file where I manage my authentication methods and variables. There are three different methods here:
-
checkBiometricsAvailability()
this method checks if the user has biometrics active. If theBiometryType
is different than none, that means that it has finger or face ID activated. So it will return a true value. If not it will be false. I've decided not to manage the false value because for the end of this repo is not useful. -
requestBiometrics()
is the main one. Here we first call thecheckBiometricsAvailability()
if this returns a true then it will show the Finger or FaceId native interface. If the authentication is successful theloggedIn
variable is set to true and as is Published the view will reload to show the app. If the authentication fails, I'm managing the errors inside a switch where in the case that the authentication fails or the user cancels the authentication prompt then we call the third and last methodrequestPinAuthentication()
-
requestPinAuthentication()
this method shows a prompt and ask the user to enter their device PIN in order to authenticate their identity.
RPReplay_Final1683876188.mov
RPReplay_Final1683876188.1.mov
Please feel free and let me know what amazing things you have done.