iOS 關於TouchID指紋解鎖的實現

來源:互聯網
上載者:User

標籤:隱藏   zip   title   設定密碼   對象   edr   zed   ogr   blog   

  一直想玩玩指紋解鎖,近期時間比較閑,就研究了一下這個指紋解鎖。這個功能實現起來還是很簡單的,雖然看起來比較高大上,但是對於開發來說,越是高大上的東西一般都有封裝好的,我們只需要調用就可以實現相當秀的功能。
  廢話少說,直接貼教程。
  蘋果公司推出的指紋識別,提供的功能很少,只要注意這兩個功能即可(驗證是否支援指紋以及指紋是否正確)。

  第一步:匯入解鎖必須的標頭檔

#import <LocalAuthentication/LocalAuthentication.h>

  第二步:(這一步主要是瞭解使用的方法,可以直接跳過看第三步的代碼)

  這個方法是判斷裝置是否支援TouchID的。

12 - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none)));

  這個是用來驗證TouchID的,會有彈出框出來。

123 - (void)evaluatePolicy:(LAPolicy)policy       localizedReason:(NSString *)localizedReason                 reply:(void(^)(BOOL success, NSError * __nullable error))reply;
 

  LAContext對象,這個主要設定的屬性是localizedFallbackTitle。這個是用於設定左邊的按鈕的名稱,預設是Enter Password.當你設定為@“”時會隱藏。還可以設定localizedReason這個屬性,這是用於設定提示,表示為什麼要使用Touch ID。

  第三步:(整體代碼)

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {    //初始化內容物件    LAContext* context = [[LAContext alloc] init];    //這個設定的使用密碼的字型,當[email protected]""時,按鈕將被隱藏

    [email protected]"輸入密碼解鎖";

//這個設定的取消按鈕的內容    [email protected]"取消";    //錯誤對象    NSError* error = nil;    NSString* result = @"需要驗證您的touch ID";    //首先使用canEvaluatePolicy 判斷裝置支援狀態    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics                             error:&error]) {        NSLog(@"支援指紋識別");        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics                localizedReason:result 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);    }    }

補充:

1.touch ID 在驗證連續錯誤三次的時候,會停止touch ID的使用,需要輸入手機的密碼以解鎖。還在研究。

2.在使用的過程中,在block回調中使用了LCProgressHUD,但是介面出現了卡死的現象,通過視圖層級下,發現是由於LCProgressHUD的介面沒有清除,導致整個介面被它覆蓋,形成卡死假象。這是別人遇到的問題,Mark一下,防止以後出錯。

 

參考連結:1.http://www.jianshu.com/p/0690a96ad5f6

     2.http://blog.csdn.net/zzzzzdddddxxxxx/article/details/53513909

附件Demo:其中包含我的小demo和一個封裝好的類touchID.zip 

iOS 關於TouchID指紋解鎖的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.