iOS Touch ID 使用详情
2016-02-23 本文已影响434人
93a13ee543af
简单介绍
支持系统和机型
iOS系统的指纹识别功能最低支持的机型为iPhone 5s,最低支持系统为iOS 8,虽然安装iOS 7系统的5s机型可以使用系统提供的指纹解锁功能,但由于API并未开放,所以理论上第三方软件不可使用。
依赖框架
LocalAuthentication.framework
import <LocalAuthentication/LocalAuthentication.h>
注意事项
做iOS 8以下版本适配时,务必进行API验证,避免调用相关API引起崩溃。
使用类
LAContext指纹验证操作对象
代码实现
//初始化上下文对象
LAContext* context = [[LAContext alloc] init]; //错误对象
NSError * error = nil;
NSString * result = @"验证";
context.localizedFallbackTitle = @"123";
//判断设备是否支持touchID
BOOL isSupport = [context canEvaluatePolicy:
LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
if (isSupport) {
//指纹识别函数
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:@"用 Touch ID 登录"
reply:^(BOOL success, NSError *error) {
//如果成功
if (success) {
NSLog(@"验证成功");
}else{
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;
}
}
}
}];
} else {
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);
}
下面是LAError中每个枚举对应的含义
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,
}