iOS開發中指紋識別簡單介紹

來源:互聯網
上載者:User

標籤:通過   代碼   nullable   支付   default   ios8   輸入   對象   總結   

中指紋識別簡單介紹,在iphone系列中,是從5S以後開始有了指紋識別的功能,在ios8的時候開放的指紋驗證的介面。

所以我們在進行指紋識別應用的時候要去判斷機型以及系統的版本。

代碼如下,下面需要特別注意的其實就是LAPolicyDeviceOwnerAuthentication和LAPolicyDeviceOwnerAuthenticationWithBiometrics的區別,以及檢測系統的版本通過[UIDevice currentDevice].systemVersion.floatValue,判斷裝置是否可用Touch ID就是通過canEvaluatePolicy: error:這個方法來進行判斷。還有需要注意的是下面驗證指紋識別是否成功的操作預設都是在子線程中進行的,所以我們如果要做UI的操作要回到主線程去執行。可用利用dispatch_async(dispatch_queue_t _Nonnull queue, ^{ }) 這個函數來實現,裡面傳入主隊列即可。還有就是我們也可以根據eror的code來進行一些判斷,看使用者具體是因為什麼原因導致的錯誤,然後在作出相應的輸出。

  1. //1、判斷系統版本是不是大於等於8.0如果大於等於的話就表示可以使用指紋識別
  2. if([UIDevice currentDevice].systemVersion.floatValue>=8.0)
  3. {
  4. //判斷是否可以使用指紋識別的功能,是在5S之後才可以進行使用的
  5. //建立LA對象的上下文
  6. LAContext * context = [[LAContext alloc]init];
  7. //判斷裝置是否支援指紋識別
  8. //Evaluate 表示評估的意思
  9. //Policy表示的是策略
  10. //用來檢查當前裝置是否可用touchID
  11. if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil])
  12. {
  13. //LAPolicyDeviceOwnerAuthentication如果我們三次指紋輸入的都錯了,就會彈出密碼框,如果不進行密碼輸入。再次進來還可以有兩次機會驗證指紋如果都
  14. 錯誤還會繼續彈出系統密碼框讓你輸入 如果你沒輸入touch ID就會被鎖定,而LAPolicyDeviceOwnerAuthenticationWithBiometrics不會彈出輸入系統
  15. 的密碼框,輸入三次錯誤之後,預設不會做任何處理,我們還可以重新再點擊指紋識別進行輸入,但是如果還是輸入錯誤兩次之後touch id就會被鎖定
  16. //表示可以使用指紋識別技術
  17. [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"請驗證指紋進行支付" reply:^
  18. (BOOL success, NSError * _Nullable error) {
  19. //裡面是在子線程中執行的,所以要更新UI的話,肯定是需要回到主線程去執行的
  20. //判斷是否成功
  21. if(success)
  22. {
  23. NSLog(@"%@",[NSThread currentThread]);
  24. NSLog(@"驗證成功");
  25. }
  26. else
  27. {
  28. NSLog(@"驗證失敗");
  29. }
  30. NSLog(@"%@",[NSThread currentThread]);
  31. NSLog(@"%@",error);
  32. if(error)
  33. {
  34. if(error.code==-2)
  35. {
  36. dispatch_async(dispatch_get_main_queue(), ^{
  37. UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"指紋驗證取消" message:@""
  38. preferredStyle:UIAlertControllerStyleAlert];
  39. UIAlertAction * action = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault
  40. handler:^(UIAlertAction * _Nonnull action) {
  41. NSLog(@"---------");
  42. }];
  43. UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
  44. handler:^(UIAlertAction * _Nonnull action) {
  45. NSLog(@"hhhhhh");
  46. }];
  47. [vc addAction:action];
  48. [vc addAction:action1];
  49. [self presentViewController:vc animated:YES completion:nil];
  50. });
  51. }
  52. else if(error.code==-1)
  53. {
  54. dispatch_async(dispatch_get_main_queue(), ^{
  55. UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"指紋已經輸錯3次" message:
  56. @"你還有兩次機會" preferredStyle:UIAlertControllerStyleAlert];
  57. UIAlertAction * action = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault
  58. handler:^(UIAlertAction * _Nonnull action) {
  59. NSLog(@"---------");
  60. }];
  61. [vc addAction:action];
  62. [self presentViewController:vc animated:YES completion:nil];
  63. });
  64. }
  65. }
  66. }];
  67. }
  68. }
  69. else
  70. {
  71. NSLog(@"對不起,系統版本過低");
  72. }
複製代碼

總結

以上所述是小編給大家介紹的iOS開發中指紋識別簡單介紹,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對網站的支援!

http://www.kmjdad.com/
http://www.jnsjzyy.com/
http://www.czhkwl.com/
http://www.express-o2o.com/
http://www.gzjindao.com/
http://www.chumingchuanmeiyishu.com/
http://www.thcxb.com/
http://www.xingguangkeji.com/
http://www.gdrhsy.com/
http://www.clhuiji.com/
http://www.nxjianye.com/
http://www.tjmingsheng.com/
http://www.gangguan022.com/
http://www.zyjbp.com/
http://www.qianhangmy.com/
http://www.tzminbell.com/

iOS開發中指紋識別簡單介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.