【发布时间】:2019-10-17 01:06:14
【问题描述】:
我是第一次在 iOS 上尝试生物认证。
我的 touch id 验证码运行良好。但如果 touch id 失败,我想使用设备 PIN 进行身份验证。但这仅在第二次触摸 ID 尝试失败后才有效。第一次,当它失败时,会显示一个带有“尝试密码”按钮的警报。但是当触摸它时,它并没有去屏幕输入设备密码,而是再次显示输入触摸ID警报。
现在,如果触摸 ID 再次失败并且我触摸了输入密码按钮。它会进入屏幕输入设备 PIN。
但为什么它第一次不起作用?来自 Apple 文档:
后备按钮最初是隐藏的。对于 Face ID,在第一个之后 认证尝试不成功,将提示用户尝试 再次面容 ID 或取消。后退按钮显示在 第二次失败的面容 ID 尝试。 对于 Touch ID,后备按钮 在第一次不成功的 Touch ID 尝试后显示。
我发现它可以与 google pay 等应用配合使用。我在这里做错了什么。
这是我的代码。
public partial class AuthenticationViewController : UIViewController
{
private LAContext context;
public AuthenticationViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
if (UserDefaultsManager.RememberMe)
TryAuthenticate();
else
AppDelegate.Instance.GotoLoginController();
}
private void TryAuthenticate()
{
context = new LAContext();
NSError error = null;
if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0) &&
context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out error)) {
// Biometry is available on the device
context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics,
"Unlock SmartFHR", HandleLAContextReplyHandler);
} else {
// Biometry is not available on the device
if (error != null) {
HandleLAContextReplyHandler(false, error);
} else {
TryDevicePinAuthentication(error);
}
}
}
private void TryDevicePinAuthentication(NSError error)
{
if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthentication, out error)) {
context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthentication,
"Unlock SmartFHR", HandleLAContextReplyHandler);
}
}
private void HandleLAContextReplyHandler(bool success, NSError error)
{
DispatchQueue.MainQueue.DispatchAsync(() => {
if (success) {
ContinueAfterAuthSuccess();
return;
}
switch (error.Code) {
case (long)LAStatus.UserCancel:
AppDelegate.Instance.GotoLoginController(true);
break;
case (long)LAStatus.UserFallback:
case (long)LAStatus.BiometryNotEnrolled:
case (long)LAStatus.BiometryNotAvailable:
TryDevicePinAuthentication(error);
break;
}
});
}
private void ContinueAfterAuthSuccess()
{
if (Storyboard.InstantiateViewController("SplashController") is SplashController vc)
AppDelegate.Instance.Window.RootViewController = vc;
}
}
当第一次 touch id 尝试失败并且我点击 Try Password 按钮时,我看到它调用了带有错误代码 LAStatus.UserFallback 的 HandleLAContextReplyHandler。
【问题讨论】:
标签: ios xamarin.ios biometrics touch-id