How can I observe when user authenticates the app with biometrics?

384 views Asked by At

Simply in code I use it like this:

let context = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)
    context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "jjj") { success, error in
        print(error)
        print(success)
    }
}

Then user can see:

enter image description here

Everything is fine until user tap Cancel. Then I display label:

"Please use biometrics to authenticate". NOW I need to get a callback after user was authenticated at any time after first try was cancelled. How can I detect this?

2

There are 2 answers

1
matt On BEST ANSWER

You don’t need a “callback” for this. If the user refuses authentication in response to the dialog, the only way authentication can happen is in Settings, i.e. outside your app. So just check for authentication every time your app comes to the foreground.

0
Chuong Tran On

Try with code Obj-C, I think that Swift is the same logic

self.context = [[LAContext alloc] init];
[self.context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
        localizedReason:strMessage
                  reply:^(BOOL success, NSError *error) {

                      dispatch_async(dispatch_get_main_queue(), ^{

                          if (error) {
                              if (error.code == LAErrorUserFallback) {
                                  //Do some thing
                              }else if (error.code == LAErrorAuthenticationFailed) {
                                  //User authen failed
                              }else if (error.code == LAErrorUserCancel) {
                                  //User cancel
                              }else{
                                  //Something wrong...
                              }
                              return;
                          }

                          if (success) {
                             //Success
                          } else {
                            //Failed
                              return;
                          }
                      });

                  }];