1、基本策略说明
typedef NS_ENUM(NSInteger, LAPolicy)
{
LAPolicyDeviceOwnerAuthenticationWithBiometrics NS_ENUM_AVAILABLE(NA, 8_0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0) = kLAPolicyDeviceOwnerAuthenticationWithBiometrics,
LAPolicyDeviceOwnerAuthentication NS_ENUM_AVAILABLE(10_11, 9_0) = kLAPolicyDeviceOwnerAuthentication
} NS_ENUM_AVAILABLE(10_10, 8_0) __WATCHOS_AVAILABLE
LAPolicyDeviceOwnerAuthenticationWithBiometrics :
允许用户用系统指纹、自己的密码<可以不是系统密码>进行登录操作-不局限于指纹;
LAPolicyDeviceOwnerAuthentication :
只允许用户通过系统指纹、系统密码进行登录校验;
上面二者使用场景不同;
2、验证时间
设置验证通过后在一个时间段内免验证;
@property (nonatomic) NSTimeInterval touchIDAuthenticationAllowableReuseDuration NS_AVAILABLE(NA, 9_0) __WATCHOS_UNAVAILABLE __TVOS_UNAVAILABLE;
一、基本使用
1、引入框架
#import <LocalAuthentication/LocalAuthentication.h>
2、添加功能代码引用
通过如下方法可以进行基本的指纹功能的使用:
LAContext *context = [LAContext new];
//第一次验证失败之后,弹出来的提示框的提示语
context.localizedFallbackTitle = @"忘记密码?";
NSError *error = nil;
if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
NSLog(@"Chenfy-error--:%@",error);
return;
}
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Chenfy-登录验证" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[self showMessage:@"yes--"];
}];
return;
}
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel: {
NSLog(@"系统取消授权,如其他APP切入");
break;
}
case LAErrorUserCancel: {
NSLog(@"用户取消验证Touch ID");
break;
}
case LAErrorAuthenticationFailed: {
NSLog(@"授权失败");
break;
}
case LAErrorPasscodeNotSet: {
NSLog(@"系统未设置密码");
break;
}
case LAErrorTouchIDNotAvailable: {
NSLog(@"设备Touch ID不可用,例如未打开");
break;
}
case LAErrorTouchIDNotEnrolled: {
NSLog(@"设备Touch ID不可用,用户未录入");
break;
}
case LAErrorUserFallback: {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"用户选择输入密码,切换主线程处理");
}];
break;
}
default: {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"其他情况,切换主线程处理");
}];
break;
}
}
}];
二、参数解读
1、接口说明
//决定某个策略在该设备上是否可用
/// Determines if a particular policy can be evaluated.
///
/// @return YES if the policy can be evaluated, NO otherwise.
- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));
2、指纹验证
/// Evaluates the specified policy.
///
/// @see LAError
///
/// Typical error codes returned by this call are:
/// @li LAErrorUserFallback if user tapped the fallback button
/// @li LAErrorUserCancel if user has tapped the Cancel button
/// @li LAErrorSystemCancel if some system event interrupted the evaluation (e.g. Home button pressed).
- (void)evaluatePolicy:(LAPolicy)policy
localizedReason:(NSString *)localizedReason
reply:(void(^)(BOOL success, NSError * __nullable error))reply;
3、使上下文失效
/// Invalidates the context.
///
///Invalidating a context that has been already invalidated has no effect.
- (void)invalidate NS_AVAILABLE(10_11, 9_0);
三、有待了解
- (void)evaluateAccessControl:(SecAccessControlRef)accessControl
operation:(LAAccessControlOperation)operation
localizedReason:(NSString *)localizedReason
reply:(void(^)(BOOL success, NSError * __nullable error))reply
NS_AVAILABLE(10_11, 9_0) __WATCHOS_AVAILABLE(3.0) __TVOS_UNAVAILABLE;