簡介
蘋果從iPhone5S開始,具有指紋識別技術(shù)额衙,從iOS8.0之后蘋果允許第三方 App 使用 Touch ID進(jìn)行身份驗證。指紋識別Touch ID提供3+2共5次指紋識別機(jī)會(3次識別失敗后,彈出的指紋驗證框會消失,同時會報錯code = -1舔琅,然后點擊指紋會再次彈框可驗證兩次),如果五次指紋識別全部錯誤洲劣,就需要手動輸入數(shù)字密碼备蚓,數(shù)字密碼可以輸入6次,如果6次輸入的數(shù)字密碼都錯誤囱稽,系統(tǒng)會停止驗證星著,一定的間隔后才能再次輸入密碼驗證,而且間隔會隨著輸入的次數(shù)增長粗悯。
使用方法
1.首先導(dǎo)入框架LocalAuthentication
2.判斷系統(tǒng)版本,最低iOS 8.0
3.創(chuàng)建驗證對象上下文LAContext
4.判斷指紋識別技術(shù)是否可用canEvaluatePolicy
5.如果可用同欠,開始調(diào)用方法開始使用指紋識別
#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: 指紋識別出現(xiàn)時的提示文字
[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]
LAPolicyDeviceOwnerAuthenticationWithBiometrics ,指紋授權(quán)使用衫哥, 當(dāng)設(shè)備不具有Touch ID的功能茎刚,或者在系統(tǒng)設(shè)置中沒有設(shè)置開啟指紋,授權(quán)將會失敗撤逢。當(dāng)指紋驗證3+2次都沒有通過的時候指紋驗證就會被鎖定膛锭,就需要先進(jìn)行數(shù)字密碼的解鎖才能繼續(xù)使用指紋密碼。
-
LAPolicyDeviceOwnerAuthentication蚊荣,指紋和數(shù)字密碼的授權(quán)使用初狰,當(dāng)指紋可用且沒有被鎖定,授權(quán)后會進(jìn)入指紋密碼驗證互例。不然的話會進(jìn)入數(shù)字密碼驗證的頁面奢入。當(dāng)系統(tǒng)數(shù)字密碼沒有設(shè)置不可用的時候,授權(quán)失敗媳叨。如果數(shù)字密碼輸入不正確腥光,連續(xù)6次輸入數(shù)字密碼都不正確后,會停用鑒定過一定的間隔后才能使用糊秆,間隔時間依次增長武福,如圖:
- 彈出的指紋驗證框的兩個按鈕的標(biāo)題,可以通過下面的方法修改:
context.localizedCancelTitle = @"取消"; context.localizedFallbackTitle = @"輸入密碼";
-
localizedReason:是用來設(shè)置彈出框的提示內(nèi)容的痘番,一般寫的是使用原因捉片,如圖:
錯誤情況
官方文檔中給出的錯誤情況,有以下幾種夫偶,如圖:
簡單介紹幾種常出現(xiàn)的錯誤
3次驗證失敗后報錯
Error Domain=com.apple.LocalAuthentication Code=-1 "Application retry limit exceeded." UserInfo={NSLocalizedDescription=Application retry limit exceeded
用戶取消
Error Domain=com.apple.LocalAuthentication Code=-2
用戶在彈出的指紋驗證框中界睁,點擊輸入密碼
Error Domain=com.apple.LocalAuthentication Code=-3 "Fallback authentication mechanism selected."
可以判斷這種錯誤 error.code == -3,進(jìn)行后面的操作
設(shè)備沒有設(shè)置指紋報錯
Error Domain=com.apple.LocalAuthentication Code=-7 "No fingers are enrolled with Touch ID."
3+2次指紋驗證都失敗后報錯兵拢,這種情況指紋解鎖會被鎖定翻斟,輸入數(shù)字密碼真確后才能再次進(jìn)行指紋驗證
Error Domain=com.apple.LocalAuthentication Code=-8 "Biometry is locked out."
注意事項
識別成功后在主線程上處理
識別成功后,不是在主線程上说铃,如果不切換到主線程上访惜,后面的不會及時處理。如果不寫后面canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication判斷腻扇,在iOS 10 上會遇到一個問題就是债热,在你驗證了3+2次指紋都沒成功之后,系統(tǒng)不會彈出輸入密碼的驗證頁面幼苛,點擊指紋驗證按鈕也沒有反應(yīng)窒篱。但是在iOS9中5次驗證失敗之后點擊再次驗證,系統(tǒng)會彈出輸入數(shù)字密碼的頁面。
最后
附上帶有指紋墙杯、手勢和數(shù)字密碼解鎖的例子配并。傳送門 GitHub