指紋識別,指紋識別手機

來源:互聯網
上載者:User

指紋識別,指紋識別手機

現在,在要求安全與效率兼得的時候,普通密碼已不能滿足我們的要求,指紋識別就這樣誕生了。

每個人都有自己專屬的指紋,在需要支付等輸入密碼的地方,我們只需輕輕一按即可,避免了輸入密碼的繁瑣步驟,更加安全,而且媽媽再也不用擔心我們忘記密碼。

 

好了,不閑扯了,開始正題。

其實,聽著高大上,實現起來特別簡單,因為蘋果已經幫我們封裝好了,我們只需要簡單的調用就好了。

1、首先,我們需要匯入標頭檔:

#import <LocalAuthentication/LocalAuthentication.h>

2、接著,我們需要判斷我們的裝置是否支援指紋識別(iPhone5s+,iOS8.0+)

     接下來,判斷目前使用者是否是機主即可,完事,是不是so easy啊。

 1 - (IBAction)biologyAction:(id)sender { 2      3     LAContext *context = [[LAContext alloc] init]; 4     NSError *error = nil; 5     NSString *reason = @"我們需要驗證您的指紋來確認您的身份"; 6      7     // 判斷設定是否支援指紋識別(iPhone5s+、iOS8+支援) 8     if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){ 9         10         // 指紋識別只判斷目前使用者是否是機主11         [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError * _Nullable error) {12             if(success){13                 NSLog(@"指紋認證成功");14             }15             else{16                 NSLog(@"指紋認證失敗");17                 NSLog(@"錯誤碼:%zd",error.code);18                 NSLog(@"出錯資訊:%@",error);19                 // 錯誤碼 error.code20                 // -1: 連續三次指紋識別錯誤21                 // -2: 在TouchID對話方塊中點擊了取消按鈕22                 // -3: 在TouchID對話方塊中點擊了輸入密碼按鈕23                 // -4: TouchID對話方塊被系統取消,例如按下Home或者電源鍵24                 // -8: 連續五次指紋識別錯誤,TouchID功能被鎖定,下一次需要輸入系統密碼25             }26         }];27     }28     else{29         NSLog(@"TouchID裝置不可用");30         NSLog(@"錯誤碼:%zd",error.code);31         NSLog(@"出錯資訊:%@",error);32     }33 }

 

下面貼出Swift3.0版對應的代碼,原理就不說了,和上面的一樣,只是將OC代碼翻譯成了Swift3.0版而已。

 1 import UIKit 2 import LocalAuthentication 3  4 class ViewController: UIViewController { 5      6     override func viewDidLoad() { 7         super.viewDidLoad() 8     } 9     10     @IBAction func yanzhengAction(_ sender: AnyObject) {11         12         let context = LAContext()13         let reason = "我們需要您的指紋來驗證您的身份"14         var error:NSError?15         16         if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error:&error){17             context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: { (isSuc, error) in18                 if isSuc{19                     print("指紋驗證成功")20                 }21                 else{22                     print("指紋驗證失敗")23                     print("錯誤資訊:\(error)")24                     // 錯誤碼 error.code25                     // -1: 連續三次指紋識別錯誤26                     // -2: 在TouchID對話方塊中點擊了取消按鈕27                     // -3: 在TouchID對話方塊中點擊了輸入密碼按鈕28                     // -4: TouchID對話方塊被系統取消,例如按下Home或者電源鍵29                     // -8: 連續五次指紋識別錯誤,TouchID功能被鎖定,下一次需要輸入系統密碼30                 }31             })32         }33         else{34             print("TouchID設定不支援")35             print("錯誤碼:\(error!.code)")36             print("錯誤資訊:\(error)")37         }38     }39 }

 

相關文章

聯繫我們

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