一、兩種鑒定方式
LAPolicyDeviceOwnerAuthenticationWithBiometrics :生物指紋識別。驗證彈框有兩個按鈕,第一個是取消按鈕,第二個按鈕可以自定義標題名稱(輸入密碼)。只有在第一次指紋驗證失敗后才會出現(xiàn)第二個按鈕,這種鑒定方式的第二個按鈕的功能自定義。前三次指紋驗證失敗,指紋驗證框不再彈出。再次重新進入驗證,還有兩次驗證機會,如果還是驗證失敗,TOUCH ID 被鎖住不再繼續(xù)彈出指紋驗證框。以后的每次驗證都將會彈出設備密碼輸入框直至輸入正確的設備密碼方可解除TOUCH ID鎖。
LAPolicyDeviceOwnerAuthentication:生物指紋識別或系統(tǒng)密碼驗證。如果TOUCH ID 可用,且已經錄入指紋,則優(yōu)先調用指紋驗證。其次是調用系統(tǒng)密碼驗證,如果沒有開啟設備密碼,則不可以使用這種驗證方式。指紋識別驗證失敗三次將彈出設備密碼輸入框,如果不進行密碼輸入。再次進來還可以有兩次機會驗證指紋,如果都失敗則TOUCH ID被鎖住,以后每次進來驗證都是調用系統(tǒng)的設備密碼直至輸入正確的設備密碼方可解除TOUCH ID鎖。
二、鑒權錯誤信息類型
1. 驗證(指紋/密碼)不能開啟的錯誤信息:
- LAErrorPasscodeNotSet : 設備密碼未設置
- LAErrorTouchIDNotAvailable : TOUCH ID不可用
- LAErrorTouchIDNotEnrolled : 指紋未錄入
- LAErrorTouchIDLockout : TOUCH ID被鎖定
- LAErrorAppCancel : APP調用了
- (void)invalidate方法使LAContext失效 - LAErrorInvalidContext : 實例化的
LAContext對象失效,再次調用evaluation...方法則會彈出此錯誤信息
2. 其他錯誤信息:
- LAErrorAuthenticationFailed : 鑒定失敗
- LAErrorUserCancel : 用戶取消
- LAErrorUserFallback : 用戶選擇輸入密碼
- LAErrorSystemCancel : 系統(tǒng)取消(如:另外一個應用進入前臺)
三、Show code
- (void)authentication
{
// 初始化上下文對象
LAContext* context = [[LAContext alloc] init];
NSError* error = nil;
NSString* result = @"需要您的支付進行支付";
context.localizedFallbackTitle = @"快捷支付";
NSLog(@"data before authentication = %@",[context evaluatedPolicyDomainState]);
// 首先使用canEvaluatePolicy 判斷設備支持狀態(tài)
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
// 支持指紋驗證
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
if (success) {
//驗證成功,主線程處理UI
NSLog(@"驗證成功...");
NSLog(@"data after authentication = %@",[context evaluatedPolicyDomainState]);
}else{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"Authentication was cancelled by the system");
//切換到其他APP,系統(tǒng)取消驗證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;
}
case LAErrorAuthenticationFailed:
{
NSLog(@"Authentication Failed");
break;
}
case LAErrorTouchIDLockout:
{
NSLog(@"TOUCH ID is locked out");
break;
}
case LAErrorAppCancel:
{
NSLog(@"app cancle the authentication");
break;
}
case LAErrorInvalidContext:
{
NSLog(@"context is invalidated");
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情況,切換主線程處理
}];
break;
}
}
}
}];
}else {
NSLog(@"%@",error.localizedDescription);
//不支持指紋識別,LOG出錯誤詳情
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;
}
}
}
}

a1
第一次進來驗證,第二行的文字 需要您的指紋進行支付 可以自定,此時第二個按鈕隱藏,第一次驗證失敗后才顯示。

a2
第一次指紋驗證失敗,第二個按鈕(標題自定義,默認為:輸入密碼)顯示出來,第一種驗證方式的此按鈕功能自定,第二種驗證方式的此按鈕點擊會調用設備密碼輸入框。

a3
設備密碼輸入框