實用原理:
指紋識別技術(shù)就是把一個人同他的指紋對應(yīng)起來,通過比較他的指紋和預(yù)先保存的指紋進行比較,就可以驗證他的真實身份。
指紋識別概念闡述:http://baike.so.com/doc/6246764-6460171.html
在iOS中的發(fā)展過程:
蘋果從iPhone5S開始,具有指紋識別技術(shù)。但是,是從iOS8.0之后程序員具有使用指紋識別的權(quán)利——蘋果允許第三方 App 使用 Touch ID 實現(xiàn)免密碼登陸。
蘋果在iPhone 5s上采用的指紋識別技術(shù)來自AuthenTec。2012年6月,蘋果收購了這家公司。在被蘋果收購之前,AuthenTec是全球最大的指紋識別芯片供應(yīng)商,擁有名為TurePrint的專利技術(shù),可以讀取皮膚表皮之下的真皮層信息。
實際應(yīng)用:
目前QQ、微信、支付寶等主流APP大都已支持指紋登錄或指紋支付。
使用時涉及關(guān)鍵點:
- iOS提供了LocalAuthentication框架,以便我們使用指紋識別。
- 指紋識別Touch ID提供3+2共5次指紋識別機會,如果五次指紋識別全部錯誤,就需要手動輸入密碼。
代碼技術(shù)流程:
首先導(dǎo)入框架#import <LocalAuthentication/LocalAuthentication.h>
- 判斷系統(tǒng)版本
- LAContext : 本地驗證對象上下文
- 判斷生物識別技術(shù)是否可用canEvaluatePolicy
- 如果可用,開始調(diào)用方法開始使用指紋識別
注意:
1、代碼中最好做錯誤處理
2、如果指紋識別成功后,有操作需要更新UI,那么一定在主線程中。
具體看代碼吧!
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //1. 判斷系統(tǒng)版本 if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
//2. LAContext : 本地驗證對象上下文
LAContext *context = [LAContext new];
//3. 判斷是否可用
//Evaluate: 評估 Policy: 策略,方針
//LAPolicyDeviceOwnerAuthenticationWithBiometrics: 允許設(shè)備擁有者使用生物識別技術(shù)
if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
NSLog(@"對不起, 指紋識別技術(shù)暫時不可用");
}
//4. 開始使用指紋識別
//localizedReason: 指紋識別出現(xiàn)時的提示文字, 一般填寫為什么使用指紋識別
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"開啟了指紋識別, 將打開隱藏功能" reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"指紋識別成功");
// 指紋識別成功,回主線程更新UI,彈出提示框
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"指紋識別成功" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
[alertView show];
});
}
if (error) {
// 錯誤的判斷chuli
if (error.code == -2) {
NSLog(@"用戶取消了操作");
// 取消操作,回主線程更新UI,彈出提示框
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"用戶取消了操作" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
[alertView show];
});
} else {
NSLog(@"錯誤: %@",error);
// 指紋識別出現(xiàn)錯誤,回主線程更新UI,彈出提示框
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:error delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil];
[alertView show];
}
}
}];
} else {
NSLog(@"對不起, 該手機不支持指紋識別");
}
}`