前言

TouchID已经出来一段时间了,感觉大家已经不再陌生,它是苹果公司在iOS 7中引入并在iPhone 5s上使用的新特性,基于一个Local Authentication的新框架,允许用户解锁设备和在App Store购物的生物识别技术。
它的强大毋庸置疑,在微信,支付宝等等的App中,我们也在市场的使用它,极大的便利我们生活等,在这篇文章中,我会封装一个TouchID类,方便大家在以后使用。

代码

Local Authentication这个框架的核心类便是LAContext类,我们会实例化一个LAContext类来进行操作。

  //初始化上下文对象
    LAContext* context = [[LAContext alloc] init];
    //错误对象
    NSError* error = nil;

如果框架无法验证,它抛出一个错误。有多种原因的设备不能够认证。

LAErrorTouchIDNotAvailable 该设备不具有指纹传感器
LAErrorPasscodeNotSet 没有密码的设备,这意味着触摸ID是禁用上设置
LAErrorTouchIDNotEnrolled 有一种密码设定,但该设备还没有被任何指纹配置

其实也就是说,我们需要先去验证设备是否支持TouchID的功能,在老的机型上,比如4,4s,显然是不具备的。

[context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]

我们用上面这句代码验证,失败则会返回刚才的三种信息。

如果成功, 我们则会经行下一步的操作,看看验证指纹的成功与失败

[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
               localizedReason:localizedReason
                         reply:
        ^(BOOL succes, NSError *error) {
            if (succes) {
                //验证成功,返回主线程处理
                NSLog(@"验证成功");
                dispatch_async(dispatch_get_main_queue(), ^{
                    backSucces(succes);
                });

            } else {
                NSLog(@"验证失败");
                NSLog(@"%@",error.localizedDescription);
                dispatch_async(dispatch_get_main_queue(), ^{
                    backFailure(error.code);
                });
            }
        }];

其实整个过程并不难, 这里我用block返回了LAError枚举, 本来我想自己自定义枚举进行返回,如下代码

typedef NS_ENUM(NSInteger, TouchIdValidationResult)
{
    kTouchIdSystemCancel,   // 切换到其他APP
    kTouchIdUserCancel,     // 用户取消验证Touch ID
    kTouchIdUserFallback,   // 用户点击其他按钮
    kTouchIdNotEnrolled,    // 不支持TouchId机型
    kTouchIdPasscodeNotSet, // 没有设置验证密码
    kTouchIdOther           // 其他
};

但我发现,没有什么比自带的LAError枚举更加详细了,同时他也给出了各个枚举的说明含义,如下

typedef NS_ENUM(NSInteger, LAError)
{
    /// Authentication was not successful, because user failed to provide valid credentials.
    LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,

    /// Authentication was canceled by user (e.g. tapped Cancel button).
    LAErrorUserCancel           = kLAErrorUserCancel,

    /// Authentication was canceled, because the user tapped the fallback button (Enter Password).
    LAErrorUserFallback         = kLAErrorUserFallback,

    /// Authentication was canceled by system (e.g. another application went to foreground).
    LAErrorSystemCancel         = kLAErrorSystemCancel,

    /// Authentication could not start, because passcode is not set on the device.
    LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,

    /// Authentication could not start, because Touch ID is not available on the device.
    LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,

    /// Authentication could not start, because Touch ID has no enrolled fingers.
    LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,

    /// Authentication was not successful, because there were too many failed Touch ID attempts and
    /// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating
    /// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.
    LAErrorTouchIDLockout   NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout,

    /// Authentication was canceled by application (e.g. invalidate was called while
    /// authentication was in progress).
    LAErrorAppCancel        NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel,

    /// LAContext passed to this call has been previously invalidated.
    LAErrorInvalidContext   NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
} NS_ENUM_AVAILABLE(10_10, 8_0);

你若觉得系统枚举过于复杂,你当然也可以自定义,我在封装的类中保留自定义枚举,方便修改。

封装类在使用的时候,直接调用方法即可,但是返回失败的时候需要自行判断。如下

- (void)clickTouchID
{
    [[MTTouchIdTool sharedInstance]evaluatePolicy:@"这是测试的Touch ID" fallbackTitle:@"输入密码" SuccesResult:^{
        NSLog(@"验证成功");
    } FailureResult:^(LAError result) {
        switch (result) {
            case LAErrorSystemCancel:
            {
                 NSLog(@"切换到其他APP");
                break;
            }
            case LAErrorUserCancel:
            {
                NSLog(@"用户取消验证Touch ID");

                break;
            }
            case LAErrorTouchIDNotEnrolled:
            {
                NSLog(@"TouchID is not enrolled");
                break;
            }
            case LAErrorUserFallback:
            {

                NSLog(@"用户选择输入密码");

                break;
            }
            default:
            {

                NSLog(@"其他情况");

                break;
            }

        }
    }];
}

使用结果如下图,经行验证

验证错误,出现输入密码按钮

结尾

TouchID使用起来非常的方便,封装类的下载地址 点击这里

若有建议和bug请及时联系我,非常感谢。


丶伊眸冷

静水流深,沧笙踏歌;三生阴晴圆缺,一朝悲欢离合。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

我不是机器人*