在IOS的開(kāi)發(fā)過(guò)程中,簡(jiǎn)要介紹了指紋識(shí)別技術(shù)。隨著iPhone系列中指紋識(shí)別功能的開(kāi)始,IOS的程序員也要簡(jiǎn)要了解指紋識(shí)別技術(shù),今天就讓武林技術(shù)頻道小編帶你一起來(lái)學(xué)習(xí)iOS之開(kāi)發(fā)指紋識(shí)別的操作方法!
所以我們?cè)谶M(jìn)行指紋識(shí)別應(yīng)用的時(shí)候要去判斷機(jī)型以及系統(tǒng)的版本。
代碼如下,下面需要特別注意的其實(shí)就是LAPolicyDeviceOwnerAuthentication和LAPolicyDeviceOwnerAuthenticationWithBiometrics的區(qū)別,以及檢測(cè)系統(tǒng)的版本通過(guò)[UIDevice currentDevice].systemVersion.floatValue,判斷設(shè)備是否可用Touch ID就是通過(guò)canEvaluatePolicy: error:這個(gè)方法來(lái)進(jìn)行判斷。還有需要注意的是下面驗(yàn)證指紋識(shí)別是否成功的操作默認(rèn)都是在子線程中進(jìn)行的,所以我們?nèi)绻鯱I的操作要回到主線程去執(zhí)行??捎美?code>dispatch_async(dispatch_queue_t _Nonnull queue, ^{ }) 這個(gè)函數(shù)來(lái)實(shí)現(xiàn),里面?zhèn)魅胫麝?duì)列即可。還有就是我們也可以根據(jù)eror的code來(lái)進(jìn)行一些判斷,看用戶具體是因?yàn)槭裁丛驅(qū)е碌腻e(cuò)誤,然后在作出相應(yīng)的輸出。
//1、判斷系統(tǒng)版本是不是大于等于8.0如果大于等于的話就表示可以使用指紋識(shí)別 if([UIDevice currentDevice].systemVersion.floatValue>=8.0) { //判斷是否可以使用指紋識(shí)別的功能,是在5S之后才可以進(jìn)行使用的 //創(chuàng)建LA對(duì)象的上下文 LAContext * context = [[LAContext alloc]init]; //判斷設(shè)備是否支持指紋識(shí)別 //Evaluate 表示評(píng)估的意思 //Policy表示的是策略 //用來(lái)檢查當(dāng)前設(shè)備是否可用touchID if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) { //LAPolicyDeviceOwnerAuthentication如果我們?nèi)沃讣y輸入的都錯(cuò)了,就會(huì)彈出密碼框,如果不進(jìn)行密碼輸入。再次進(jìn)來(lái)還可以有兩次機(jī)會(huì)驗(yàn)證指紋如果都 錯(cuò)誤還會(huì)繼續(xù)彈出系統(tǒng)密碼框讓你輸入 如果你沒(méi)輸入touch ID就會(huì)被鎖定,而LAPolicyDeviceOwnerAuthenticationWithBiometrics不會(huì)彈出輸入系統(tǒng) 的密碼框,輸入三次錯(cuò)誤之后,默認(rèn)不會(huì)做任何處理,我們還可以重新再點(diǎn)擊指紋識(shí)別進(jìn)行輸入,但是如果還是輸入錯(cuò)誤兩次之后touch id就會(huì)被鎖定 //表示可以使用指紋識(shí)別技術(shù) [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"請(qǐng)驗(yàn)證指紋進(jìn)行支付" reply:^ (BOOL success, NSError * _Nullable error) { //里面是在子線程中執(zhí)行的,所以要更新UI的話,肯定是需要回到主線程去執(zhí)行的 //判斷是否成功 if(success) { NSLog(@"%@",[NSThread currentThread]); NSLog(@"驗(yàn)證成功"); } else { NSLog(@"驗(yàn)證失敗"); } NSLog(@"%@",[NSThread currentThread]); NSLog(@"%@",error); if(error) { if(error.code==-2) { dispatch_async(dispatch_get_main_queue(), ^{UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"指紋驗(yàn)證取消" message:@"" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction * action = [UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {NSLog(@"---------");}];UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {NSLog(@"hhhhhh");}];[vc addAction:action];[vc addAction:action1];[self presentViewController:vc animated:YES completion:nil];}); } else if(error.code==-1) { dispatch_async(dispatch_get_main_queue(), ^{UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"指紋已經(jīng)輸錯(cuò)3次" message: @"你還有兩次機(jī)會(huì)" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction * action = [UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {NSLog(@"---------");}];[vc addAction:action];[self presentViewController:vc animated:YES completion:nil];}); } } }]; } } else { NSLog(@"對(duì)不起,系統(tǒng)版本過(guò)低"); }以上是小編介紹的iOS之開(kāi)發(fā)指紋識(shí)別的操作方法,希望對(duì)你有幫助。如果您有任何問(wèn)題,請(qǐng)留言,小編會(huì)及時(shí)回復(fù)您。非常感謝您對(duì)武林技術(shù)頻道網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選