iOS指紋識別登錄流程及實現(xiàn)
最近一直在追青云志,總覺得電視劇沒有小說來的精彩。是的,大咖們演技堪稱驚艷,劇情改編也很緊湊,但不得不說很多東西單靠演是達(dá)不到的,主人公每一刻的內(nèi)心也只能在小說中才能看的貼切(為了裝X,哥不惜二百兩買了一沓正版典藏版)。
看過的童鞋知道,張小凡手中的法寶,是由攝魂與嗜血珠以張小凡精血為媒淬煉而成。而且此法寶,有一特大優(yōu)秀品質(zhì),那就是除了與張小凡有血緣關(guān)系的人之外,即便你有通天本領(lǐng)也不能操控,忠誠如此夫復(fù)何求啊,說到這里大概就扯到正題了,對的,此法寶自帶安全驗證功能,類似我們今天的密碼校驗與 紋識別驗證 功能。
指紋識別簡析蘋果設(shè)計的iOS是以安全性為核心的,不管是沙盒機制,還是代碼簽名等,他們的最終目的都是為了安全。
iOS 安全架構(gòu)圖
自iPhone 5S始,蘋果公司推出了全新生物安全識別技術(shù)---指紋識別驗證(Touch ID)。使得我們可以更快、更輕松地對設(shè)備進行安全的訪問。可貴的是,Touch ID做到了從任意角度讀取指紋數(shù)據(jù),克服了基于密碼進行鎖定的不便。除此之外,蘋果還加入必須進行密碼校驗的場景,進一步確保安全,例如【1】:
剛開機或重啟;
超過 48 小時未解鎖設(shè)備;
設(shè)備收到了遠(yuǎn)程鎖定命令;
五次未能成功匹配指紋;
進入Touch ID設(shè)置模塊或更新新指紋;
最重要的一點,蘋果公司提供Touch ID給第三方應(yīng)用程序使用,程序只會收到認(rèn)證是否成功的通知,而無法訪問 Touch ID 或與已注冊指紋相關(guān)的數(shù)據(jù),這一點對安全而言尤為重要。
為了獲得更高的安全性,很多銀行類、支付類APP都集成了指紋、手勢等二次驗證功能。今天我們就重點來談?wù)凾ouch ID集成到APP的具體流程及實現(xiàn)。
流程分析指紋登錄流程:
首次登錄.png
二次啟動后識別登錄:
指紋驗證登錄.png
使用過指紋登錄的朋友,大概都知道上面的流程。這個業(yè)務(wù)實現(xiàn)的難點在于,首次登錄成功并啟用指紋授權(quán)--->退出APP后--->二次啟動APP,如何判斷是否要啟用指紋登錄驗證呢?這時就需要我們對數(shù)據(jù)持久化和數(shù)據(jù)共享有較深的理解,很多APP開發(fā)者,在開發(fā) 登錄保持 的時候,大都會使用持久化數(shù)據(jù)的方式,存儲 成功登錄 的標(biāo)記。但對于安全性較高的APP,每次重新啟動時都會校驗登錄狀態(tài),單靠持久化數(shù)據(jù)是不夠的。
我的解決方案是:
通過三個數(shù)據(jù)進行 登錄保持 ,
loginState:持久化數(shù)據(jù),用于存儲用戶登錄成功,未激活狀態(tài);
startAutoLoginState:持久化數(shù)據(jù),是否開啟指紋識別授權(quán);
isAppCurrentLoginState:共享數(shù)據(jù),登錄激活狀態(tài),該狀態(tài)的特點,每次重新啟動APP都會重新初始化數(shù)據(jù)。
首次登錄:
三個數(shù)據(jù)變化情況,
狀態(tài)loginStatestartAutoLoginStateisAppCurrentLoginState登錄之前null或NOnull或NONO登錄成功YESnull或NOYES啟用指紋授權(quán)YESYESYES不啟用授權(quán)YESNOYES二次驗證登錄(指紋登錄):
三個數(shù)據(jù)變化情況,
如果loginState和startAutoLoginState同為YES,即可進行指紋登錄驗證,以下為數(shù)據(jù)變化情況;
狀態(tài)loginStatestartAutoLoginStateisAppCurrentLoginState驗證之前YESYESNO驗證失敗NOYESNO驗證成功YESYESYES否則,重新登錄。
核心代碼實現(xiàn)判斷設(shè)備是否支持指紋識別
/** * 判斷設(shè)備是否支持指紋識別 */ - (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; // 判斷設(shè)備是否支持指紋識別功能 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]; // 這個屬性是設(shè)置指紋輸入失敗之后的彈出框的選項 myContext.localizedFallbackTitle = @'忘記密碼'; NSError *authError = nil; NSString *myLocalizedReasonString = @'請按住Home鍵完成驗證'; // MARK: 判斷設(shè)備是否支持指紋識別 if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) { if(success) { NSLog(@'指紋認(rèn)證成功');weakSelf.helper.isAppCurrentLoginState = YES;weakSelf.logoutBtnAction.hidden = NO;weakSelf.userInfo.text = @'仁伯安'; } else {weakSelf.helper.isAppCurrentLoginState = NO; NSLog(@'指紋認(rèn)證失敗,%@',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(@'授權(quán)失敗'); // -1 連續(xù)三次指紋識別錯誤 } 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(@'取消授權(quán),如其他應(yīng)用切入,用戶自主'); // -4 TouchID對話框被系統(tǒng)取消,例如按下Home或者電源鍵 } break; case LAErrorPasscodeNotSet: // Authentication could not start, because passcode is not set on the device. { NSLog(@'設(shè)備系統(tǒng)未設(shè)置密碼'); // -5 } break; case LAErrorTouchIDNotAvailable: // Authentication could not start, because Touch ID is not available on the device { NSLog(@'設(shè)備未設(shè)置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 用戶連續(xù)多次進行Touch ID驗證失敗,Touch ID被鎖,需要用戶輸入密碼解鎖,先Touch ID驗證密碼 { NSLog(@'Touch ID被鎖,需要用戶輸入密碼解鎖'); // -8 連續(xù)五次指紋識別錯誤,TouchID功能被鎖定,下一次需要輸入系統(tǒng)密碼 } break; case LAErrorAppCancel: // Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然來了電話,電話應(yīng)用進入前臺,APP被掛起啦'); { NSLog(@'用戶不能控制情況下APP被掛起'); // -9 } break; case LAErrorInvalidContext: // LAContext passed to this call has been previously invalidated. { NSLog(@'LAContext傳遞給這個調(diào)用之前已經(jīng)失效'); // -10 } break; #else #endif default: {[[NSOperationQueue mainQueue] addOperationWithBlock:^{ NSLog(@'其他情況,切換主線程處理');}]; break; }} }}]; } else { NSLog(@'設(shè)備不支持指紋'); 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
相關(guān)文章:
1. nodejs報digital envelope routines::unsupported錯誤的最新解決方法2. 匹配模式 - XSL教程 - 43. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁4. 詳解JS前端使用迭代器和生成器原理及示例5. WML語法大全與相關(guān)介紹第1/3頁6. 詳解CSS偽元素的妙用單標(biāo)簽之美7. 使用css實現(xiàn)全兼容tooltip提示框8. asp中response.write("中文")或者js中文亂碼問題9. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)10. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向
