ios8指紋識別

簡介

蘋果從iPhone5S開始,具備指紋識別技術,從iOS8.0以後蘋果容許第三方 App 使用 Touch ID進行身份驗證。指紋識別Touch ID提供3+2共5次指紋識別機會(3次識別失敗後,彈出的指紋驗證框會消失,同時會報錯code = -1,而後點擊指紋會再次彈框可驗證兩次),若是五次指紋識別所有錯誤,就須要手動輸入數字密碼,數字密碼能夠輸入6次,若是6次輸入的數字密碼都錯誤,系統會中止驗證,必定的間隔後才能再次輸入密碼驗證,並且間隔會隨着輸入的次數增加。框架

使用方法

1.首先導入框架LocalAuthentication
2.判斷系統版本,最低iOS 8.0
3.建立驗證對象上下文LAContext
4.判斷指紋識別技術是否可用canEvaluatePolicy
5.若是可用,開始調用方法開始使用指紋識別async

#import <LocalAuthentication/LocalAuthentication.h>



//指紋按鈕 - (void)showFingerprintTouch { //系統支持,最低iOS 8.0 if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) { LAContext * context = [[LAContext alloc] init]; NSError * error; if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { //localizedReason: 指紋識別出現時的提示文字 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"指紋解鎖" reply:^(BOOL success, NSError * _Nullable error) { if (success) { //識別成功 dispatch_async(dispatch_get_main_queue(), ^{ //在主線程中,處理 ...... }); } else if (error) { NSLog(@"LAPolicyDeviceOwnerAuthenticationWithBiometrics -- %@",error); } }]; } else if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]) { [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"密碼解鎖" reply:^(BOOL success, NSError * _Nullable error){ NSLog(@"LAPolicyDeviceOwnerAuthentication -- %@", error); }]; } NSLog(@" --- %@ ", error); } }

代碼解析

指紋識別的策略

[context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil] [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil]ui

  • LAPolicyDeviceOwnerAuthenticationWithBiometrics ,指紋受權使用, 當設備不具備Touch ID的功能,或者在系統設置中沒有設置開啓指紋,受權將會失敗。當指紋驗證3+2次都沒有經過的時候指紋驗證就會被鎖定,就須要先進行數字密碼的解鎖才能繼續使用指紋密碼。
  • LAPolicyDeviceOwnerAuthentication,指紋和數字密碼的受權使用,當指紋可用且沒有被鎖定,受權後會進入指紋密碼驗證。否則的話會進入數字密碼驗證的頁面。當系統數字密碼沒有設置不可用的時候,受權失敗。若是數字密碼輸入不真確,連續6次輸入數字密碼都不真確後,會停用鑑定過必定的間隔後才能使用,間隔時間依次增加,如圖:


  • 彈出的指紋驗證框的兩個按鈕的標題,能夠經過下面的方法修改:
    context.localizedCancelTitle = @"取消"; context.localizedFallbackTitle = @"輸入密碼";
  • localizedReason:是用來設置彈出框的提示內容的,通常寫的是是用緣由,如圖:

     

     
     
     
     
    //詳細介紹

    //建立LAContext(指紋驗證操做對象)lua

        LAContext *context = [LAContext new];spa

        

        //這個屬性是設置指紋輸入 失敗 以後的彈出框的選項線程

        context.localizedFallbackTitle = @"沒有忘記密碼";3d

        

        NSError *error = nil;code

    //    使用canEvaluatePolicy  ,判斷設備支持狀態對象

        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {blog

            NSLog(@"支持指紋識別");

            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請按home鍵指紋解鎖" reply:^(BOOL success, NSError * _Nullable error) {

                if (success) {

                    NSLog(@"驗證成功 刷新主界面");

                }else{

                    NSLog(@"錯誤:%@",error.localizedDescription);

                    switch (error.code) {

                        case LAErrorSystemCancel:

                        {

                            NSLog(@"系統取消受權,如其餘APP切入");

                            break;

                        }

                        case LAErrorUserCancel:

                        {

                            NSLog(@"用戶取消驗證Touch ID");

                            break;

                        }

                        case LAErrorAuthenticationFailed:

                        {

                            NSLog(@"受權失敗");

                            break;

                        }

                        case LAErrorPasscodeNotSet:

                        {

                            NSLog(@"系統未設置密碼");

                            break;

                        }

                        case LAErrorTouchIDNotAvailable:

                        {

                            NSLog(@"設備Touch ID不可用,例如未打開");

                            break;

                        }

                        case LAErrorTouchIDNotEnrolled:

                        {

                            NSLog(@"設備Touch ID不可用,用戶未錄入");

                            break;

                        }

                        case LAErrorUserFallback:

                        {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                NSLog(@"用戶選擇輸入密碼,切換主線程處理");

                            }];

                            break;

                        }

                        default:

                        {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                NSLog(@"其餘狀況,切換主線程處理");

                            }];

                            break;

                        }

                    }

                }

            }];

        }else{

            NSLog(@"不支持指紋識別");

            switch (error.code) {

                case LAErrorTouchIDNotEnrolled:

                {

                    NSLog(@"TouchID is not enrolled");

                    break;

                }

                case LAErrorPasscodeNotSet:

                {

                    NSLog(@"A passcode has not been set");

                    break;

                }

                default:

                {

                    NSLog(@"TouchID not available");

                    break;

                }

            }

            

            NSLog(@"%@",error.localizedDescription);

        }

相關文章
相關標籤/搜索