iOS Touch ID 简易开发教程

基础知识

支持系统和机型

iOS系统的指纹识别功能最低支持的机型为iPhone 5s,最低支持系统为iOS 8,虽然安装iOS 7系统的5s机型可使用系统提供的指纹解锁功能,但因为API并未开放,因此理论上第三方软件不可以使用。segmentfault

依赖框架

LocalAuthentication.frameworkapp

#import <LocalAuthentication/LocalAuthentication.h>

注意事项

iOS 8如下版本适配时,务必进行API验证,避免调用相关API引发崩溃。框架

使用类

LAContext 指纹验证操做对象ui

代码

- (void)authenticateUser
{
    //初始化上下文对象
    LAContext* context = [[LAContext alloc] init];
    //错误对象
    NSError* error = nil;
    NSString* result = @"Authentication is needed to access your notes.";
    
    //首先使用canEvaluatePolicy 判断设备支持状态
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        //支持指纹验证
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
            if (success) {
                //验证成功,主线程处理UI
            }
            else
            {
                NSLog(@"%@",error.localizedDescription);
                switch (error.code) {
                    case LAErrorSystemCancel:
                    {
                        NSLog(@"Authentication was cancelled by the system");
                        //切换到其余APP,系统取消验证Touch ID
                        break;
                    }
                    case LAErrorUserCancel:
                    {
                        NSLog(@"Authentication was cancelled by the user");
                        //用户取消验证Touch ID
                        break;
                    }
                    case LAErrorUserFallback:
                    {
                        NSLog(@"User selected to enter custom password");
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //用户选择输入密码,切换主线程处理
                        }];
                        break;
                    }
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                           //其余状况,切换主线程处理 
                        }];
                        break;
                    }
                }
            }
        }];
    }
    else
    {
        //不支持指纹识别,LOG出错误详情
        
        switch (error.code) {
            case LAErrorTouchIDNotEnrolled:
            {
                NSLog(@"TouchID is not enrolled");
                break;
            }
            case LAErrorPasscodeNotSet:
            {
                NSLog(@"A passcode has not been set");
                break;
            }
            default:
            {
                NSLog(@"TouchID not available");
                break;
            }
        }
        
        NSLog(@"%@",error.localizedDescription);
    }
}
typedef NS_ENUM(NSInteger, LAError)
{
    //受权失败
    LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
    
    //用户取消Touch ID受权
    LAErrorUserCancel           = kLAErrorUserCancel,
    
    //用户选择输入密码
    LAErrorUserFallback         = kLAErrorUserFallback,
    
    //系统取消受权(例如其余APP切入)
    LAErrorSystemCancel         = kLAErrorSystemCancel,
    
    //系统未设置密码
    LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,

    //设备Touch ID不可用,例如未打开
    LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,
    
    //设备Touch ID不可用,用户未录入
    LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);

操做流程

首先判断系统版本,iOS 8及以上版本执行-(void)authenticateUser方法,方法自动判断设备是否支持和开启Touch IDthis

iOS 9

感谢秋儿指出iOS 9加入了三种新的错误类型。lua

/// 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

其中,LAErrorTouchIDLockout是在8.0中也会出现的状况,但并未归为单独的错误类型,这个错误出现,源自用户屡次连续使用Touch ID失败,Touch ID被锁,须要用户输入密码解锁,这个错误的交互LocalAuthentication.framework已经封装好了,不须要开发者关心。线程

LAErrorAppCancelLAErrorSystemCancel类似,都是当前软件被挂起取消了受权,可是前者是用户不能控制的挂起,例如忽然来了电话,电话应用进入前台,APP被挂起。后者是用户本身切到了别的应用,例如按home键挂起。code

LAErrorInvalidContext很好理解,就是受权过程当中,LAContext对象被释放掉了,形成的受权失败。对象

相关文章
相关标签/搜索