iOS touchID的使用

touchID 及 API介绍

一、头文件

#import <LocalAuthentication/LocalAuthentication.h>

二、系统版本

iOS 8以上支持touchID

 //首先判断版本
if (NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {
      NSLog(@"系统版本不支持TouchID");
      return;
}    

三、API LAContext

方法均为实例方法,需要先初始化实例

    LAContext *context = [[LAContext alloc] init];

1.属性

// 取消按钮的标题,10以后可以使用
@property (nonatomic, nullable, copy) NSString *localizedCancelTitle

// 9以后可以使用 用来验证是否有变更,参加4的描述
@property (nonatomic, nullable, readonly) NSData *evaluatedPolicyDomainState;

/* 默认第一次只显示取消按钮,当失败一次之后,会显示fallbackTitle按钮,点击则跳到输入密码页面 */
// 允许后退按钮标题自定义。如果设置为空字符串,按钮将被隐藏。
@property (nonatomic, nullable, copy) NSString *localizedFallbackTitle;

// 该方法只在8-9系统可以使用,设置最大失败尝试次数
@property (nonatomic, nullable) NSNumber *maxBiometryFailures

// 这个属性应该是类似于支付宝的指纹开启应用,
// 如果你打开他解锁之后,按Home键返回桌面,
// 再次进入支付宝是不需要录入指纹的。
// 因为这个属性可以设置一个时间间隔,在时间间隔内是不需要再次录入。
// 默认是0秒,最长可以设置5分钟。
@property (nonatomic) NSTimeInterval touchIDAuthenticationAllowableReuseDuration

2.LAPolicy

是一个枚举,标识设备所有者将使用生物识别方法(Touch ID或Face ID)进行身份验证。


/**
iOS 8 开始,同kLAPolicyDeviceOwnerAuthenticationWithBiometrics 
使用该设备的TouchID进行验证,当输入TouchID验证5次失败后,TouchID被锁定,只能通过锁屏后解锁设备时输入正确的解锁密码来解锁TouchID
*/
LAPolicyDeviceOwnerAuthenticationWithBiometrics,

/**
iOS 9 开始,同kLAPolicyDeviceOwnerAuthentication
使用该设备的TouchID或设备密码进行验证,当输入TouchID验证5次失败后,TouchID被锁定,会触发设备密码页面进行验证
*/
LAPolicyDeviceOwnerAuthentication,

    
/**
同kLAPolicyDeviceOwnerAuthenticationWithWatch
手表专用,用户可以双击手表侧边按钮解锁
*/
LAPolicyDeviceOwnerAuthenticationWithWatch,
    
/**
手表专用,同kLAPolicyDeviceOwnerAuthenticationWithBiometricsOrWatch
*/
LAPolicyDeviceOwnerAuthenticationWithBiometricsOrWatch,

3.canEvaluatePolicy

iOS 8 的方法

/**
确定是否可以使用该策略这个方法检查是否可以使用touchID或者faceID
返回YES表示可以使用
*/
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)))
 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
 }

4.evaluatedPolicyDomainState

@property (nonatomic, nullable, readonly) NSData *evaluatedPolicyDomainState API_AVAILABLE(macos(10.11), ios(9.0)) API_UNAVAILABLE(watchos, tvos);

用于判断设备上的指纹是否被更改,在LAContext被创建的时候,evaluatedPolicyDomainState才生效,可在TouchID验证成功时,将它记录下来,用于下次使用TouchID时校验,提高安全性。

5.evaluatePolicy

该方法是对TouchID进行验证,Block回调中如果success为YES则验证成功,为NO验证失败,并对error进行解析.

/**
policy: touchID的使用策略
localizedReason:请求原因,会在touchID弹框的消息位置标识出来提示用户
reply:验证结果
*/
- (void)evaluatePolicy:(LAPolicy)policy
       localizedReason:(NSString *)localizedReason
                 reply:(void(^)(BOOL success, NSError * __nullable error))reply

例子:

[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) {
            if(success)
            {
                NSLog(@"指纹认证成功");
            }
            else
            {
                NSLog(@"指纹认证失败,%@",error.description);

                NSLog(@"%ld", (long)error.code); // 错误码 error.code
                switch (error.code)
                {
                    case LAErrorAuthenticationFailed: // Authentication was not successful, because user failed to provide valid credentials
                    {
                        NSLog(@"授权失败"); // -1 连续三次指纹识别错误
                    }
                        break;
                    case LAErrorUserCancel: // Authentication was canceled by user (e.g. tapped Cancel button)
                    {
                        NSLog(@"用户取消验证Touch ID"); // -2 在TouchID对话框中点击了取消按钮

                    }
                        break;
                    case LAErrorUserFallback: // Authentication was canceled, because the user tapped the fallback button (Enter Password)
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"用户选择输入密码,切换主线程处理"); // -3 在TouchID对话框中点击了输入密码按钮
                        }];

                    }
                        break;
                    case LAErrorSystemCancel: // Authentication was canceled by system (e.g. another application went to foreground)
                    {
                        NSLog(@"取消授权,如其他应用切入,用户自主"); // -4 TouchID对话框被系统取消,例如按下Home或者电源键
                    }
                        break;
                    case LAErrorPasscodeNotSet: // Authentication could not start, because passcode is not set on the device.

                    {
                        NSLog(@"设备系统未设置密码"); // -5
                    }
                        break;
                    case LAErrorTouchIDNotAvailable: // Authentication could not start, because Touch ID is not available on the device
                    {
                        NSLog(@"设备未设置Touch ID"); // -6
                    }
                        break;
                    case LAErrorTouchIDNotEnrolled: // Authentication could not start, because Touch ID has no enrolled fingers
                    {
                        NSLog(@"用户未录入指纹"); // -7
                    }
                        break;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
                    case LAErrorTouchIDLockout: //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 用户连续多次进行Touch ID验证失败,Touch ID被锁,需要用户输入密码解锁,先Touch ID验证密码
                    {
                        NSLog(@"Touch ID被锁,需要用户输入密码解锁"); // -8 连续五次指纹识别错误,TouchID功能被锁定,下一次需要输入系统密码
                    }
                        break;
                    case LAErrorAppCancel: // Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然来了电话,电话应用进入前台,APP被挂起啦");
                    {
                        NSLog(@"用户不能控制情况下APP被挂起"); // -9
                    }
                        break;
                    case LAErrorInvalidContext: // LAContext passed to this call has been previously invalidated.
                    {
                        NSLog(@"LAContext传递给这个调用之前已经失效"); // -10
                    }
                        break;
#else
#endif
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"其他情况,切换主线程处理");
                        }];
                        break;
                    }
                }
            }
        }];
posted @ 2021-01-24 15:17  absty_guo  阅读(512)  评论(0编辑  收藏  举报