如何在 Xamarin 中处理 iOS 生物识别身份验证中的错误情况?

Posted

技术标签:

【中文标题】如何在 Xamarin 中处理 iOS 生物识别身份验证中的错误情况?【英文标题】:How to handle error cases in Biometric authentication for iOS in Xamarin? 【发布时间】:2018-06-04 05:02:17 【问题描述】:
if (context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out AuthError))
    var replyHandler = new LAContextReplyHandler((success, error) => 
        this.InvokeOnMainThread(()=> 
            if(success)
            
                Console.WriteLine("You logged in!");
                PerformSegue("AuthenticationSegue", this);
            
            else
            
                // Show fallback mechanism here
            
        );
    );
    context.EvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, myReason, replyHandler);
;

我想根据错误的类型来处理else条件下的错误情况。

【问题讨论】:

【参考方案1】:

您可以从返回的NSError 中获取失败代码,并针对LAStatus 代码进行处理:

switch (error.Code)

    case (long)LAStatus.AuthenticationFailed:
        ~~~
        break;
    case (long)LAStatus.UserCancel:
        ~~~
        break;
    ~~~
    default:
        break;

LAStatus(去掉了弃用):

public enum LAStatus : long

    Success,
    AuthenticationFailed = -1L,
    UserCancel = -2L,
    UserFallback = -3L,
    SystemCancel = -4L,
    PasscodeNotSet = -5L,
    AppCancel = -9L,
    InvalidContext = -10L,
    BiometryNotAvailable = -6L,
    BiometryNotEnrolled = -7L,
    BiometryLockout = -8L,
    NotInteractive = -1004L

各种代码的描述可以使用LAError.Code:

https://developer.apple.com/documentation/localauthentication/laerror.code

【讨论】:

好的,我去看看。

以上是关于如何在 Xamarin 中处理 iOS 生物识别身份验证中的错误情况?的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin.Forms 警告:尝试使用 iOS 图像/手势识别器在其视图不在窗口层次结构中的 * 上呈现 *

Xamarin 上的 Bing Speech API/语音服务(无 REST,sdk websocket 来处理连续识别)

使用 Xamarin.iOS (C#) 在 iOS 中进行后台处理:我应该使用哪种方法?

如何在 xamarin 中为 iOS 12 添加凭据提供程序应用程序扩展

Xamarin Forms - iOS 上的键盘覆盖条目(文本字段)

单击处理程序 Xamarin