蘋果從iPhone5S開始,具備指紋識別技術,從iOS8.0以後蘋果容許第三方 App 使用 Touch ID進行身份驗證。指紋識別Touch ID提供3+2共5次指紋識別機會(3次識別失敗後,彈出的指紋驗證框會消失,同時會報錯code = -1,而後點擊指紋會再次彈框可驗證兩次),若是五次指紋識別所有錯誤,就須要手動輸入數字密碼,數字密碼能夠輸入6次,若是6次輸入的數字密碼都錯誤,系統會中止驗證,必定的間隔後才能再次輸入密碼驗證,並且間隔會隨着輸入的次數增加。框架
1.首先導入框架LocalAuthentication
2.判斷系統版本,最低iOS 8.0
3.建立驗證對象上下文LAContext
4.判斷指紋識別技術是否可用canEvaluatePolicy
5.若是可用,開始調用方法開始使用指紋識別async
#import <LocalAuthentication/LocalAuthentication.h>
//指紋按鈕 - (void)showFingerprintTouch { //系統支持,最低iOS 8.0 if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) { LAContext * context = [[LAContext alloc] init]; NSError * error; if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { //localizedReason: 指紋識別出現時的提示文字 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指紋解鎖" reply:^(BOOL success, NSError * _Nullable error) { if (success) { //識別成功 dispatch_async(dispatch_get_main_queue(), ^{ //在主線程中,處理 ...... }); } else if (error) { NSLog(@"LAPolicyDeviceOwnerAuthenticationWithBiometrics -- %@",error); } }]; } else if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]) { [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"密碼解鎖" reply:^(BOOL success, NSError * _Nullable error){ NSLog(@"LAPolicyDeviceOwnerAuthentication -- %@", error); }]; } NSLog(@" --- %@ ", error); } }
[context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil] [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]
ui
context.localizedCancelTitle = @"取消"; context.localizedFallbackTitle = @"輸入密碼";
//建立LAContext(指紋驗證操做對象)lua
LAContext *context = [LAContext new];spa
//這個屬性是設置指紋輸入 失敗 以後的彈出框的選項線程
context.localizedFallbackTitle = @"沒有忘記密碼";3d
NSError *error = nil;code
// 使用canEvaluatePolicy ,判斷設備支持狀態對象
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {blog
NSLog(@"支持指紋識別");
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請按home鍵指紋解鎖" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"驗證成功 刷新主界面");
}else{
NSLog(@"錯誤:%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"系統取消受權,如其餘APP切入");
break;
}
case LAErrorUserCancel:
{
NSLog(@"用戶取消驗證Touch ID");
break;
}
case LAErrorAuthenticationFailed:
{
NSLog(@"受權失敗");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"系統未設置密碼");
break;
}
case LAErrorTouchIDNotAvailable:
{
NSLog(@"設備Touch ID不可用,例如未打開");
break;
}
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"設備Touch ID不可用,用戶未錄入");
break;
}
case LAErrorUserFallback:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"用戶選擇輸入密碼,切換主線程處理");
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"其餘狀況,切換主線程處理");
}];
break;
}
}
}
}];
}else{
NSLog(@"不支持指紋識別");
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);
}