自iPhone 5S始,蘋果公司推出了全新生物安全識別技術---指紋識別驗證(Touch ID)。使得我們可以更快、更輕松地對設備進行安全的訪問。可貴的是,Touch ID做到了從任意角度讀取指紋數據,克服了基于密碼進行鎖定的不便。除此之外,蘋果還加入必須進行密碼校驗的場景,進一步確保安全。
最重要的一點,蘋果公司提供Touch ID給第三方應用程序使用,程序只會收到認證是否成功的通知,而無法訪問 Touch ID 或與已注冊指紋相關的數據,這一點對安全而言尤為重要。
為了獲得更高的安全性,很多銀行類、支付類APP都集成了指紋、手勢等二次驗證功能。今天我們就利用一個demo來了解下如何利用指紋識別:
1.首先判斷機型是否支持指紋識別或者指紋識別是否可用,注意:首先導入LocalAuthentication框架及其頭文件,我們可以在success回調里面做下一步的事情,比如支付寶里面的指紋支付,識別成功以后再做下一步的result界面跳轉
代碼如下:
LAContext*context = [LAContextnew];//上下文
NSError*error;
//判斷是否支持指紋識別
BOOLisInteligence = [contextcanEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
error:&error];
if(isInteligence) {
NSString*myLocalizedReasonString =@"請按住Home鍵完成驗證";
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOLsuccess, NSError *_Nullableerror) {
if(success) {
//在這里拿到識別結果的回調
//do something。。。。
}else{
switch(error.code)
{
caseLAErrorAuthenticationFailed:// Authentication was not successful, because user failed to provide valid credentials
{
NSLog(@"授權失敗");// -1 連續三次指紋識別錯誤
}
break;
caseLAErrorUserCancel:// Authentication was canceled by user (e.g. tapped Cancel button)
{
NSLog(@"用戶取消驗證Touch ID");// -2 在TouchID對話框中點擊了取消按鈕
}
break;
caseLAErrorUserFallback:// Authentication was canceled, because the user tapped the fallback button (Enter Password)
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"用戶選擇輸入密碼,切換主線程處理");// -3 在TouchID對話框中點擊了輸入密碼按鈕
}];
}
break;
caseLAErrorSystemCancel:// Authentication was canceled by system (e.g. another application went to foreground)
{
NSLog(@"取消授權,如其他應用切入,用戶自主");// -4 TouchID對話框被系統取消,例如按下Home或者電源鍵
}
break;
caseLAErrorPasscodeNotSet:// Authentication could not start, because passcode is not set on the device.
{
NSLog(@"設備系統未設置密碼");// -5
}
break;
caseLAErrorTouchIDNotAvailable:// Authentication could not start, because Touch ID is not available on the device
{
NSLog(@"設備未設置Touch ID");// -6
}
break;
caseLAErrorTouchIDNotEnrolled:// Authentication could not start, because Touch ID has no enrolled fingers
{
NSLog(@"用戶未錄入指紋");// -7
}
break;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
caseLAErrorTouchIDLockout://Authentication was not successful, because there were too many failed Touch ID attempts and Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite 用戶連續多次進行Touch ID驗證失敗,Touch ID被鎖,需要用戶輸入密碼解鎖,先Touch ID驗證密碼
{
NSLog(@"Touch ID被鎖,需要用戶輸入密碼解鎖");// -8 連續五次指紋識別錯誤,TouchID功能被鎖定,下一次需要輸入系統密碼
}
break;
caseLAErrorAppCancel:// Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然來了電話,電話應用進入前臺,APP被掛起啦");
{
NSLog(@"用戶不能控制情況下APP被掛起");// -9
}
break;
caseLAErrorInvalidContext:// LAContext passed to this call has been previously invalidated.
{
NSLog(@"LAContext傳遞給這個調用之前已經失效");// -10
}
break;
#else
#endif
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"其他情況,切換主線程處理");
}];
break;
}
}
}];
}else{
UIAlertController *alertVC= [UIAlertController alertControllerWithTitle:@"不支持指紋識別"
message:nilpreferredStyle:UIAlertControllerStyleAlert];
for(inti = 0; i < 1 ; i ++) {
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定"style: UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnullaction) {
}];
[alertVC addAction:action];
}
[selfpresentViewController:alertVC animated:YEScompletion:nil];
}
}