ios開發中指紋識別簡單介紹,在iphone系列中,是從5S以后開始有了指紋識別的功能,在ios8的時候開放的指紋驗證的接口。
所以我們在進行指紋識別應用的時候要去判斷機型以及系統的版本。
代碼如下,下面需要特別注意的其實就是LAPolicyDeviceOwnerAuthentication和LAPolicyDeviceOwnerAuthenticationWithBiometrics的區別,以及檢測系統的版本通過[UIDevice currentDevice].systemVersion.floatValue,判斷設備是否可用Touch ID就是通過canEvaluatePolicy: error:這個方法來進行判斷。還有需要注意的是下面驗證指紋識別是否成功的操作默認都是在子線程中進行的,所以我們如果要做UI的操作要回到主線程去執行??捎美?code>dispatch_async(dispatch_queue_t _Nonnull queue, ^{ }) 這個函數來實現,里面傳入主隊列即可。還有就是我們也可以根據eror的code來進行一些判斷,看用戶具體是因為什么原因導致的錯誤,然后在作出相應的輸出。
//1、判斷系統版本是不是大于等于8.0如果大于等于的話就表示可以使用指紋識別
if([UIDevice currentDevice].systemVersion.floatValue>=8.0)
{
//判斷是否可以使用指紋識別的功能,是在5S之后才可以進行使用的
//創建LA對象的上下文
LAContext * context = [[LAContext alloc]init];
//判斷設備是否支持指紋識別
//Evaluate 表示評估的意思
//Policy表示的是策略
//用來檢查當前設備是否可用touchID
if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil])
{
//LAPolicyDeviceOwnerAuthentication如果我們三次指紋輸入的都錯了,就會彈出密碼框,如果不進行密碼輸入。再次進來還可以有兩次機會驗證指紋如果都
錯誤還會繼續彈出系統密碼框讓你輸入 如果你沒輸入touch ID就會被鎖定,而LAPolicyDeviceOwnerAuthenticationWithBiometrics不會彈出輸入系統
的密碼框,輸入三次錯誤之后,默認不會做任何處理,我們還可以重新再點擊指紋識別進行輸入,但是如果還是輸入錯誤兩次之后touch id就會被鎖定
//表示可以使用指紋識別技術
[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"請驗證指紋進行支付" reply:^
(BOOL success, NSError * _Nullable error) {
//里面是在子線程中執行的,所以要更新UI的話,肯定是需要回到主線程去執行的
//判斷是否成功
if(success)
{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"驗證成功");
}
else
{
NSLog(@"驗證失敗");
}
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"%@",error);
if(error)
{
if(error.code==-2)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"指紋驗證取消" message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"確認" 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:@"指紋已經輸錯3次" message:
@"你還有兩次機會" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"---------");
}];
[vc addAction:action];
[self presentViewController:vc animated:YES completion:nil];
});
}
}
}];
}
}
else
{
NSLog(@"對不起,系統版本過低");
}
總結
以上所述是小編給大家介紹的iOS開發中指紋識別簡單介紹,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。