iOS指紋識別登錄流程及實現
最近一直在追青云志,總覺得電視劇沒有小說來的精彩。是的,大咖們演技堪稱驚艷,劇情改編也很緊湊,但不得不說很多東西單靠演是達不到的,主人公每一刻的內心也只能在小說中才能看的貼切(為了裝X,哥不惜二百兩買了一沓正版典藏版)。
看過的童鞋知道,張小凡手中的法寶,是由攝魂與嗜血珠以張小凡精血為媒淬煉而成。而且此法寶,有一特大優秀品質,那就是除了與張小凡有血緣關系的人之外,即便你有通天本領也不能操控,忠誠如此夫復何求啊,說到這里大概就扯到正題了,對的,此法寶自帶安全驗證功能,類似我們今天的密碼校驗與 紋識別驗證 功能。
指紋識別簡析蘋果設計的iOS是以安全性為核心的,不管是沙盒機制,還是代碼簽名等,他們的最終目的都是為了安全。
iOS 安全架構圖
自iPhone 5S始,蘋果公司推出了全新生物安全識別技術---指紋識別驗證(Touch ID)。使得我們可以更快、更輕松地對設備進行安全的訪問。可貴的是,Touch ID做到了從任意角度讀取指紋數據,克服了基于密碼進行鎖定的不便。除此之外,蘋果還加入必須進行密碼校驗的場景,進一步確保安全,例如【1】:
剛開機或重啟;
超過 48 小時未解鎖設備;
設備收到了遠程鎖定命令;
五次未能成功匹配指紋;
進入Touch ID設置模塊或更新新指紋;
最重要的一點,蘋果公司提供Touch ID給第三方應用程序使用,程序只會收到認證是否成功的通知,而無法訪問 Touch ID 或與已注冊指紋相關的數據,這一點對安全而言尤為重要。
為了獲得更高的安全性,很多銀行類、支付類APP都集成了指紋、手勢等二次驗證功能。今天我們就重點來談談Touch ID集成到APP的具體流程及實現。
流程分析指紋登錄流程:
首次登錄.png
二次啟動后識別登錄:
指紋驗證登錄.png
使用過指紋登錄的朋友,大概都知道上面的流程。這個業務實現的難點在于,首次登錄成功并啟用指紋授權--->退出APP后--->二次啟動APP,如何判斷是否要啟用指紋登錄驗證呢?這時就需要我們對數據持久化和數據共享有較深的理解,很多APP開發者,在開發 登錄保持 的時候,大都會使用持久化數據的方式,存儲 成功登錄 的標記。但對于安全性較高的APP,每次重新啟動時都會校驗登錄狀態,單靠持久化數據是不夠的。
我的解決方案是:
通過三個數據進行 登錄保持 ,
loginState:持久化數據,用于存儲用戶登錄成功,未激活狀態;
startAutoLoginState:持久化數據,是否開啟指紋識別授權;
isAppCurrentLoginState:共享數據,登錄激活狀態,該狀態的特點,每次重新啟動APP都會重新初始化數據。
首次登錄:
三個數據變化情況,
狀態loginStatestartAutoLoginStateisAppCurrentLoginState登錄之前null或NOnull或NONO登錄成功YESnull或NOYES啟用指紋授權YESYESYES不啟用授權YESNOYES二次驗證登錄(指紋登錄):
三個數據變化情況,
如果loginState和startAutoLoginState同為YES,即可進行指紋登錄驗證,以下為數據變化情況;
狀態loginStatestartAutoLoginStateisAppCurrentLoginState驗證之前YESYESNO驗證失敗NOYESNO驗證成功YESYESYES否則,重新登錄。
核心代碼實現判斷設備是否支持指紋識別
/** * 判斷設備是否支持指紋識別 */ - (IBAction)loginBtnAction:(UIButton *)sender{ [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@'loginState']; EVNHelper *helper = [EVNHelper shareHelper]; helper.isAppCurrentLoginState = YES; LAContext *context = [[LAContext alloc] init]; // 初始化上下文對象 NSError *error = nil; // 判斷設備是否支持指紋識別功能 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { // 支持指紋驗證 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@'登錄成功!' message:@'是否啟用指紋登錄' preferredStyle:UIAlertControllerStyleAlert];__weak typeof (self) weakSelf = self; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@'稍后' style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:NO] forKey:@'startAutoLoginState']; weakSelf.transLoginStateBlock(); // 回傳 [self dismissViewControllerAnimated:YES completion:nil];}]; UIAlertAction *startUseAction = [UIAlertAction actionWithTitle:@'啟用' style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@'startAutoLoginState']; weakSelf.transLoginStateBlock(); // 回傳 [self dismissViewControllerAnimated:YES completion:nil];}];[alertController addAction:cancelAction];[alertController addAction:startUseAction];[self presentViewController:alertController animated:YES completion:nil]; } else {[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:NO] forKey:@'startAutoLoginState']; self.transLoginStateBlock(); // 回傳 [self dismissViewControllerAnimated:YES completion:nil]; }}
指紋登錄驗證
/** * 指紋登錄驗證 */ - (void)loadAuthentication{ __weak typeof(self) weakSelf = self; LAContext *myContext = [[LAContext alloc] init]; // 這個屬性是設置指紋輸入失敗之后的彈出框的選項 myContext.localizedFallbackTitle = @'忘記密碼'; NSError *authError = nil; NSString *myLocalizedReasonString = @'請按住Home鍵完成驗證'; // MARK: 判斷設備是否支持指紋識別 if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) { if(success) { NSLog(@'指紋認證成功');weakSelf.helper.isAppCurrentLoginState = YES;weakSelf.logoutBtnAction.hidden = NO;weakSelf.userInfo.text = @'仁伯安'; } else {weakSelf.helper.isAppCurrentLoginState = NO; NSLog(@'指紋認證失敗,%@',error.description); NSLog(@'%ld', (long)error.code); // 錯誤碼 error.code switch (error.code){ case LAErrorAuthenticationFailed: // Authentication was not successful, because user failed to provide valid credentials { NSLog(@'授權失敗'); // -1 連續三次指紋識別錯誤 } break; case LAErrorUserCancel: // Authentication was canceled by user (e.g. tapped Cancel button) { NSLog(@'用戶取消驗證Touch ID'); // -2 在TouchID對話框中點擊了取消按鈕 } break; case LAErrorUserFallback: // Authentication was canceled, because the user tapped the fallback button (Enter Password) {[[NSOperationQueue mainQueue] addOperationWithBlock:^{ NSLog(@'用戶選擇輸入密碼,切換主線程處理'); // -3 在TouchID對話框中點擊了輸入密碼按鈕 }]; } break; case LAErrorSystemCancel: // Authentication was canceled by system (e.g. another application went to foreground) { NSLog(@'取消授權,如其他應用切入,用戶自主'); // -4 TouchID對話框被系統取消,例如按下Home或者電源鍵 } break; case LAErrorPasscodeNotSet: // Authentication could not start, because passcode is not set on the device. { NSLog(@'設備系統未設置密碼'); // -5 } break; case LAErrorTouchIDNotAvailable: // Authentication could not start, because Touch ID is not available on the device { NSLog(@'設備未設置Touch ID'); // -6 } break; case LAErrorTouchIDNotEnrolled: // Authentication could not start, because Touch ID has no enrolled fingers { NSLog(@'用戶未錄入指紋'); // -7 } break; #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0 case LAErrorTouchIDLockout: //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; case LAErrorAppCancel: // Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然來了電話,電話應用進入前臺,APP被掛起啦'); { NSLog(@'用戶不能控制情況下APP被掛起'); // -9 } break; case LAErrorInvalidContext: // LAContext passed to this call has been previously invalidated. { NSLog(@'LAContext傳遞給這個調用之前已經失效'); // -10 } break; #else #endif default: {[[NSOperationQueue mainQueue] addOperationWithBlock:^{ NSLog(@'其他情況,切換主線程處理');}]; break; }} }}]; } else { NSLog(@'設備不支持指紋'); NSLog(@'%ld', (long)authError.code);weakSelf.helper.isAppCurrentLoginState = NO; switch (authError.code){ case LAErrorTouchIDNotEnrolled: { NSLog(@'Authentication could not start, because Touch ID has no enrolled fingers'); break; } case LAErrorPasscodeNotSet: { NSLog(@'Authentication could not start, because passcode is not set on the device'); break; } default: { NSLog(@'TouchID not available'); break; }} }}
參考文獻:
【1】 iOS security guide ;
【2】 Apple Objective-C ;
【3】 Apple Swift API .
來自:http://www.jianshu.com/p/67fd93408517
相關文章: