Touch ID简介
Touch ID是苹果公司提供的一种将指纹用作密码的简便方式。只需轻触i主屏幕按钮,就能激活Touch ID传感器。主屏幕按钮周围的钢圈检测手指通知 Touch ID读取指纹。Touch ID 不会储存指纹的任何图像。它只存储指纹的数学表达式。
Touch ID的指纹数据存储在A7处理器的一个叫"secure enclave"协处理器上,唯一的Touch ID指纹识别器与唯一单独的A7处理器匹配。也就是说你将Touch ID拆开装到别的iPhone 5s上,Touch ID是无法使用的,因为它无法读取到A7处理器上的指纹数据。
Touch ID接口
使用Touch ID需要导入LocalAuthentication.framework,必须在装有iOS8的真机设备才能编译通过。
导入
#import <LocalAuthentication/LAContext.h>
判断当前是否有可用的Touch ID
- (BOOL)canEvaluatePolicy
{
LAContext *context = [[LAContext alloc] init];
NSError *error;
BOOL success;
// test if we can evaluate the policy, this test will tell us if Touch ID is available and enrolled
success = [context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
return success;
}
判断[context canEvaluatePolicy:error:];判断当前是否有可用Touch ID,设备没有设备没有TouchID或者TouchID未开启返回false,有TouchID并开启返回true.
调用显示验证界面
- (void)evaluatePolicy
{
LAContext *context = [[LAContext alloc] init];
__block NSString *msg;
// show the authentication UI with our reason string
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"UNLOCK_ACCESS_TO_LOCKED_FATURE", nil) reply:
^(BOOL success, NSError *authenticationError) {
if (success) {
msg =[NSString stringWithFormat:@"EVALUATE_POLICY_SUCCESS"];
} else {
msg = [NSString stringWithFormat:@"EVALUATE_POLICY_WITH_ERROR : %@",
authenticationError];
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:msg message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alertView show];
}];
}
调用[contextevaluatePolicy:localizedReason:reply]可以显示验证界面,验证完毕后有一个回调。苹果官方文档规定第二个参数localizedReason一定要(shoudbe)使用用户的当前语言呈现。
真机演示(演示指纹识别成功的例子)
测试用的代码片段
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
UIButton* btn = [[UIButton alloc] init];
[btn setTitle:@"push me!" forState:UIControlStateNormal];
CGRect frame = CGRectMake(self.view.frame.size.width/2 - 60., self.view.frame.size.height/2, 120., 80.);
[btn setFrame:frame];
[btn addTarget:self action:@selector(showTouchId) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)showTouchId
{
if ([self canEvaluatePolicy]) {
[self evaluatePolicy];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"5s ok?" message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alertView show];
}
}
#pragma mark - Tests
- (BOOL)canEvaluatePolicy
{
LAContext *context = [[LAContext alloc] init];
NSError *error;
BOOL success;
// test if we can evaluate the policy, this test will tell us if Touch ID is available and enrolled
success = [context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
return success;
}
- (void)evaluatePolicy
{
LAContext *context = [[LAContext alloc] init];
__block NSString *msg;
// show the authentication UI with our reason string
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"UNLOCK_ACCESS_TO_LOCKED_FATURE", nil) reply:
^(BOOL success, NSError *authenticationError) {
if (success) {
msg =[NSString stringWithFormat:@"EVALUATE_POLICY_SUCCESS"];
} else {
msg = [NSString stringWithFormat:@"EVALUATE_POLICY_WITH_ERROR : %@",
authenticationError];
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:msg message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alertView show];
}];
}