簡(jiǎn)介
蘋果從iPhone5S開始,具有指紋識(shí)別技術(shù),從iOS8.0之后蘋果允許第三方 App 使用 Touch ID進(jìn)行身份驗(yàn)證。指紋識(shí)別Touch ID提供3+2共5次指紋識(shí)別機(jī)會(huì)(3次識(shí)別失敗后,彈出的指紋驗(yàn)證框會(huì)消失,同時(shí)會(huì)報(bào)錯(cuò)code = -1,然后點(diǎn)擊指紋會(huì)再次彈框可驗(yàn)證兩次),如果五次指紋識(shí)別全部錯(cuò)誤,就需要手動(dòng)輸入數(shù)字密碼,數(shù)字密碼可以輸入6次,如果6次輸入的數(shù)字密碼都錯(cuò)誤,系統(tǒng)會(huì)停止驗(yàn)證,一定的間隔后才能再次輸入密碼驗(yàn)證,而且間隔會(huì)隨著輸入的次數(shù)增長(zhǎng)。
使用方法
1.首先導(dǎo)入框架LocalAuthentication
2.判斷系統(tǒng)版本,最低iOS 8.0
3.創(chuàng)建驗(yàn)證對(duì)象上下文LAContext
4.判斷指紋識(shí)別技術(shù)是否可用canEvaluatePolicy
5.如果可用,開始調(diào)用方法開始使用指紋識(shí)別
#import <LocalAuthentication/LocalAuthentication.h>
//指紋按鈕
- (void)showFingerprintTouch
{
//系統(tǒng)支持,最低iOS 8.0
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)
{
LAContext * context = [[LAContext alloc] init];
NSError * error;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error])
{
//localizedReason: 指紋識(shí)別出現(xiàn)時(shí)的提示文字
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指紋解鎖" reply:^(BOOL success, NSError * _Nullable error) {
if (success)
{
//識(shí)別成功
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);
}
}
代碼解析
指紋識(shí)別的策略
[context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil] [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]

LAPolicyDeviceOwnerAuthenticationWithBiometrics ,指紋授權(quán)使用, 當(dāng)設(shè)備不具有Touch ID的功能,或者在系統(tǒng)設(shè)置中沒有設(shè)置開啟指紋,授權(quán)將會(huì)失敗。當(dāng)指紋驗(yàn)證3+2次都沒有通過的時(shí)候指紋驗(yàn)證就會(huì)被鎖定,就需要先進(jìn)行數(shù)字密碼的解鎖才能繼續(xù)使用指紋密碼。
-
LAPolicyDeviceOwnerAuthentication,指紋和數(shù)字密碼的授權(quán)使用,當(dāng)指紋可用且沒有被鎖定,授權(quán)后會(huì)進(jìn)入指紋密碼驗(yàn)證。不然的話會(huì)進(jìn)入數(shù)字密碼驗(yàn)證的頁面。當(dāng)系統(tǒng)數(shù)字密碼沒有設(shè)置不可用的時(shí)候,授權(quán)失敗。如果數(shù)字密碼輸入不正確,連續(xù)6次輸入數(shù)字密碼都不正確后,會(huì)停用鑒定過一定的間隔后才能使用,間隔時(shí)間依次增長(zhǎng),如圖:
- 彈出的指紋驗(yàn)證框的兩個(gè)按鈕的標(biāo)題,可以通過下面的方法修改:
context.localizedCancelTitle = @"取消"; context.localizedFallbackTitle = @"輸入密碼"; -
localizedReason:是用來設(shè)置彈出框的提示內(nèi)容的,一般寫的是使用原因,如圖:
錯(cuò)誤情況
官方文檔中給出的錯(cuò)誤情況,有以下幾種,如圖:


簡(jiǎn)單介紹幾種常出現(xiàn)的錯(cuò)誤
3次驗(yàn)證失敗后報(bào)錯(cuò)
Error Domain=com.apple.LocalAuthentication Code=-1 "Application retry limit exceeded." UserInfo={NSLocalizedDescription=Application retry limit exceeded
用戶取消
Error Domain=com.apple.LocalAuthentication Code=-2
用戶在彈出的指紋驗(yàn)證框中,點(diǎn)擊輸入密碼
Error Domain=com.apple.LocalAuthentication Code=-3 "Fallback authentication mechanism selected."
可以判斷這種錯(cuò)誤 error.code == -3,進(jìn)行后面的操作
設(shè)備沒有設(shè)置指紋報(bào)錯(cuò)
Error Domain=com.apple.LocalAuthentication Code=-7 "No fingers are enrolled with Touch ID."
3+2次指紋驗(yàn)證都失敗后報(bào)錯(cuò),這種情況指紋解鎖會(huì)被鎖定,輸入數(shù)字密碼真確后才能再次進(jìn)行指紋驗(yàn)證
Error Domain=com.apple.LocalAuthentication Code=-8 "Biometry is locked out."
注意事項(xiàng)
識(shí)別成功后在主線程上處理
識(shí)別成功后,不是在主線程上,如果不切換到主線程上,后面的不會(huì)及時(shí)處理。如果不寫后面canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication判斷,在iOS 10 上會(huì)遇到一個(gè)問題就是,在你驗(yàn)證了3+2次指紋都沒成功之后,系統(tǒng)不會(huì)彈出輸入密碼的驗(yàn)證頁面,點(diǎn)擊指紋驗(yàn)證按鈕也沒有反應(yīng)。但是在iOS9中5次驗(yàn)證失敗之后點(diǎn)擊再次驗(yàn)證,系統(tǒng)會(huì)彈出輸入數(shù)字密碼的頁面。
最后
附上帶有指紋、手勢(shì)和數(shù)字密碼解鎖的例子。傳送門 GitHub


