说到本地验证,不得不提密码验证,指纹验证,Face ID也就是刷脸验证.
借用苹果的一张图来说明下认证的工作流程.
认证对比资料(密码,指纹信息,FaceID信息等),被存储在一块安全的区域.通过操作系统把认证信息(是否成功,报错信息等)发送给咱们的App.
本地生物验证的使用
// 1.首先导入本地验证框架
#import <LocalAuthentication/LocalAuthentication.h>
// 2.这是苹果文档里头的代码
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = <#String explaining why app needs authentication#>;
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
// User authenticated successfully, take appropriate action
} else {
// User did not authenticate successfully, look at error and take appropriate action
}
}];
} else {
// Could not evaluate policy; look at authError and present an appropriate message to user
}
- 需要创建一个LocalAuthentication的上下文
- evaluatePolicy表明是验证方式,大家也知道Face ID刷脸是iPhone X独占.但是iPhone X并没有指纹识别.相当于本地验证非密码方式的就只有生物验证(对于iPhone X是Face ID,对于非iPhone X的设备是指纹识别,LAPolicyDeviceOwnerAuthenticationWithBiometrics).
typedef NS_ENUM(NSInteger, LAPolicy)
{
LAPolicyDeviceOwnerAuthenticationWithBiometrics//生物验证
LAPolicyDeviceOwnerAuthentication//密码验证
}
- 这个canxxx方法,在使用AV框架调用相机的时候大家肯定都用过.调用闪光灯什么的,作用主要分两点,1.防止崩溃.2.是否可用.
typedef NS_ENUM(NSInteger, LABiometryType)
{
/// The device does not support biometry.
LABiometryTypeNone API_AVAILABLE(macos(10.13.2), ios(11.2)),
LABiometryNone API_DEPRECATED_WITH_REPLACEMENT("LABiometryTypeNone", macos(10.13, 10.13.2), ios(11.0, 11.2)) = LABiometryTypeNone,
/// The device supports Touch ID.
LABiometryTypeTouchID,
/// The device supports Face ID.
LABiometryTypeFaceID API_UNAVAILABLE(macos),//看到这个就知道Face ID上Mac无望了.
}
验证错误枚举的话都在LAError.h
这个头文件中.