Touch ID 整理

苹果官方的demo:KeychainTouchIDUsingTouchIDwithKeychainandLocalAuthentication
本帖的demo:TouchID

1)需要引入:LocalAuthentication.framework
[objc]
#import <LocalAuthentication/LocalAuthentication.h>
[/objc]
2)详见代码:
[objc]
– (IBAction)btnAction:(id)sender {

if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0)
{
LAContext *context = [LAContext new];

NSError *error;
context.localizedFallbackTitle = @"手输密码";//回退按钮。为nil,则显示“输入密码”。不使用就直接这样。

if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {

[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请用指纹识别来验证" reply:^(BOOL success, NSError *error) {

if (success) {
[self alertWithMsg:@"识别成功"];

} else {
if (error.code == kLAErrorUserFallback) {
[self alertWithMsg:@"用户手输密码"];
} else if (error.code == kLAErrorUserCancel) {
[self alertWithMsg:@"用户取消"];
} else {
[self alertWithMsg:@"验证失败"];
}
}
}];

} else {
[self alertWithMsg:@"您的设备不支持Touch ID,请输入密码"];
}

}else{
[self alertWithMsg:@"您的设备不支持Touch ID,请输入密码"];
}

}

– (void)alertWithMsg:(NSString *)msg{

//注意,不回主线程,会导致等了很久才出AlertView。
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:msg message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
});

}
[/objc]

89 thoughts on “Touch ID 整理

Ugvzkh进行回复 取消回复

电子邮件地址不会被公开。