iPhone5以上设备不支持Touch ID,iPhone5s并且iOS 8 SDK开始支持Touch ID。
配置
framework中引入LocalAuthentication, 该framework实际上只有三个头文件:
LAContext.h
LAError.h
LAPublicDefines.h
主要使用方法有两个:
// 用来判断设备是否支持Touch ID
-(BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing )error;
// 真正验证身份
-(void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;
认证失败错误类型
1.认证是不成功的,因为用户无法提供有效的指纹。
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed
2.认证取消了用户(例如:取消按钮)。
LAErrorUserCancel = kLAErrorUserCancel
3.认证被取消了,因为用户点击回退按钮(输入密码)。
LAErrorUserFallback = kLAErrorUserFallback
4.认证取消了系统(例如,另一个应用程序到前台,用户切到了其他程序)。
LAErrorSystemCancel = kLAErrorSystemCancel
5.认证无法启动,因为密码没有在设备上设置。
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet
6.无法启动身份验证,因为touch id 在此台设备上是无效的。
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable
7.认证不能启动,因为 touch id 没有录入指纹。
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled
8.认证是不成功的,因为有太多的失败的触摸使touchID被锁了。(需要解锁Touch ID,例如评估lapolicydeviceownerauthenticationwithbiometrics会要求密码为前提。)
LAErrorTouchIDLockout = kLAErrorTouchIDLockout
9.认证被取消的应用(如电话应用进入前台当前软件被动挂起,用户不能控制的挂起)。
LAErrorAppCancel = kLAErrorAppCancel
10.LAContext就是授权过程中LAContext对象被释放掉了,造成的授权失败。
LAErrorInvalidContext = kLAErrorInvalidContext
备注
1.在 iPhone5 上的时候,LAContext *myContext = [[LAContext alloc] init]; 初始化的对象为 nil。
2.在5s 上没有设置指纹密码时, error = -7 (LAErrorTouchIDNotEnrolled)。
3.输入错误指纹时.若一共有三次机会,三次全部错误后, error = -1 (LAErrorAuthenticationFailed)
4.myContext.localizedFallbackTitle,修改指纹验证弹框右侧输入密码按钮文案属性。
5.myContext.localizedCancelTitle,修改指纹验证弹框左侧取消按钮文案属性。
6.myContext.maxBiometryFailures,修改指纹验证最大次数属性。
7.认证失败错误类型7,8,9三项是iOS9加入的三种新错误类型。
代码
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = @"Restricted Area!";
//检查Touch ID是否可用
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
//获得指纹验证结果
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
// User authenticated successfully, take appropriate action
alert.title = @"Success";
alert.message = @"You have access to private content!";
[alert show];
} else {
// User did not authenticate successfully, look at error and take appropriate action
alert.title = @"Fail";
switch (error.code) {
case LAErrorUserCancel:
alert.message = @"Authentication Cancelled";
break;
case LAErrorAuthenticationFailed:
alert.message = @"Authentication Failed";
break;
case LAErrorPasscodeNotSet:
alert.message = @"Passcode is not set";
break;
case LAErrorSystemCancel:
alert.message = @"System cancelled authentication";
break;
case LAErrorUserFallback:
alert.message = @"You chosed to try password";
break;
default:
alert.message = @"You cannot access to private content!";
break;
}
[alert show];
}
}];
} else {
// Could not evaluate policy; look at authError and present an appropriate message to user
alert.title = @"Warning";
if(authError.code == LAErrorTouchIDNotEnrolled) {
alert.message = @"You do not have any fingerprints enrolled!";
}else if(authError.code == LAErrorTouchIDNotAvailable) {
alert.message = @"Your device does not support TouchID authentication!";
}else if(authError.code == LAErrorPasscodeNotSet){
alert.message = @"Your passcode has not been set";
}
[alert show];
}