当用户使用生物识别技术对应用进行身份验证时,我该如何观察?

时间:2018-10-05 13:38:42

标签: ios swift localauthentication

仅在代码中,我就这样使用它:

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

然后用户可以看到:

enter image description here

一切正常,直到用户点击Cancel。然后显示标签:

"Please use biometrics to authenticate"。现在,在首次尝试被取消后的任何时间,用户通过身份验证后,我都需要获取回调。我该如何检测?

2 个答案:

答案 0 :(得分:1)

您不需要“回调”。如果用户响应该对话框拒绝身份验证,则可以进行身份​​验证的唯一方法是在“设置”中,即在您的应用外部。因此,每当您的应用程序出现在前台时,只需检查身份验证即可。

答案 1 :(得分:-2)

尝试使用代码Obj-C,我认为Swift是相同的逻辑

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;
                          }
                      });

                  }];