iPhone5S開始,推出指紋識別
iOS8.0之后蘋果允許第三方 App 使用 Touch ID進(jìn)行身份驗(yàn)證
指紋識別Touch ID提供3+2 = 5次指紋識別機(jī)會----->3次識別失敗后,指紋驗(yàn)證框消失(會報(bào)錯code = -1)然后點(diǎn)擊指紋會再次彈框可驗(yàn)證兩次,如果五次指紋識別全部錯誤,就需要手動輸入數(shù)字密碼,數(shù)字密碼可以輸入6次,如果6次輸入密碼錯誤,系統(tǒng)停止驗(yàn)證,等待驗(yàn)證時間后會提供再次驗(yàn)證的機(jī)會,正確及驗(yàn)證成功(1次),錯誤則時間累加等待驗(yàn)證,以此類推.
iOS10-----> 5次之后有問題: 需要進(jìn)入設(shè)置中 -- TouchID與密碼, 輸入一次密碼, 就可以解開
思路:
1.導(dǎo)入#import
2.判斷iOS8及以后版本
3.創(chuàng)建本地驗(yàn)證上下文對象LAContext? ? (LocalAuthentication)
4. 判斷能否使用指紋識別 :Evaluate: 評估? Policy: 策略 LAPolicyDeviceOwnerAuthenticationWithBiometrics: 設(shè)備擁有者授權(quán) 用 生物識別技術(shù)
5.成功調(diào)用<有的情況>
主要代碼及注釋:
//1.判斷iOS8及以后的版本
if([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){
//從iPhone5S開始,出現(xiàn)指紋識別技術(shù),所以說在此處可以進(jìn)一步判斷是否是5S以后機(jī)型
//2.創(chuàng)建本地驗(yàn)證上下文對象-->這里導(dǎo)入框架LocalAuthentication
LAContext *context = [LAContext new];
// 3.判斷能否使用指紋識別
//Evaluate: 評估
//Policy: 策略
//LAPolicyDeviceOwnerAuthenticationWithBiometrics: 設(shè)備擁有者授權(quán) 用 生物識別技術(shù)
if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]){
//4.在可以使用的前提下就會調(diào)用
//localizedReason本地原因alert顯示
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請?jiān)试S設(shè)備指紋識別" reply:^(BOOL success, NSError * _Nullable error) {
? ? ? if (success) {?
? ? ? ? ? ? //此處記得在主線程中更新UI
? ? ? ? ? ? dispatch_sync(dispatch_get_main_queue(), ^{
? ? ? ? ? ? UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"? 提示? " message:@" 識別成功 " ? ? ? ? ? ? ? ? ? ? ?preferredStyle:UIAlertControllerStyleAlert];
? ? ? ? ? ? UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action){
? ? ? ? }]; ?
? ? ? ? ? ? [ac addAction:confirm];
? ? ? ? ? ? ?[self presentViewController:ac animated:true completion:^{
? ? ? ?}]; ?
? ? ?ac = nil;
? ?});
}
//不需要統(tǒng)計(jì)用戶取消
if(error.code != -2){
//? 指紋識別Touch ID提供3+2 = 5次指紋識別機(jī)會----->3次識別失敗后,指紋驗(yàn)證框消失(會報(bào)錯code = -1)然后點(diǎn)擊指紋會再次彈框可驗(yàn)證兩次,如果五次指紋識別全部錯誤,就需要手動輸入數(shù)字密碼,數(shù)字密碼可以輸入6次,如果6次輸入密碼錯誤,系統(tǒng)停止驗(yàn)證,等待驗(yàn)證時間后會提供再次驗(yàn)證的機(jī)會,正確及驗(yàn)證成功(1次),錯誤則時間累加等待驗(yàn)證,以此類推. (iOS10不一樣, 5次之后有問題: 需要進(jìn)入設(shè)置中 -- TouchID與密碼, 輸入一次密碼, 就可以解開)
? ? ? //Code=-2 "Canceled by user
? ? ? //Code=-1 "Application retry limit exceeded."
? ? ?//Code=-8 "Biometry is locked out."
? ? ? NSLog(@"error: %@", error);
? ? }?
? }];
}else{
NSLog(@"請確保(5S以上機(jī)型),TouchID未打開");
? ?}
}