iOS 指紋解鎖,蘋果已經封裝好了,只需無腦擼便可.swift
OC版本:app
引入頭文件:ide
#import <LocalAuthentication/LocalAuthentication.h>lua
1 // 2 // ViewController.m 3 // TouchID 4 // 5 // Created by Shaoting Zhou on 2018/1/23. 6 // Copyright © 2018年 Shaoting Zhou. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import <LocalAuthentication/LocalAuthentication.h> 11 12 13 @interface ViewController () 14 15 @end 16 17 @implementation ViewController 18 19 - (void)viewDidLoad { 20 [super viewDidLoad]; 21 // Do any additional setup after loading the view, typically from a nib. 22 //建立LAContext 23 LAContext *context = [LAContext new]; 24 25 //這個屬性是設置指紋輸入失敗以後的彈出框的選項 26 context.localizedFallbackTitle = @"沒有忘記密碼"; 27 28 NSError *error = nil; 29 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { 30 NSLog(@"支持指紋識別"); 31 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請按home鍵指紋解鎖" reply:^(BOOL success, NSError * _Nullable error) { 32 if (success) { 33 NSLog(@"驗證成功 刷新主界面"); 34 }else{ 35 NSLog(@"%@",error.localizedDescription); 36 switch (error.code) { 37 case LAErrorSystemCancel: 38 { 39 NSLog(@"系統取消受權,如其餘APP切入"); 40 break; 41 } 42 case LAErrorUserCancel: 43 { 44 NSLog(@"用戶取消驗證Touch ID"); 45 break; 46 } 47 case LAErrorAuthenticationFailed: 48 { 49 NSLog(@"受權失敗"); 50 break; 51 } 52 case LAErrorPasscodeNotSet: 53 { 54 NSLog(@"系統未設置密碼"); 55 break; 56 } 57 case LAErrorTouchIDNotAvailable: 58 { 59 NSLog(@"設備Touch ID不可用,例如未打開"); 60 break; 61 } 62 case LAErrorTouchIDNotEnrolled: 63 { 64 NSLog(@"設備Touch ID不可用,用戶未錄入"); 65 break; 66 } 67 case LAErrorUserFallback: 68 { 69 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 70 NSLog(@"用戶選擇輸入密碼,切換主線程處理"); 71 }]; 72 break; 73 } 74 default: 75 { 76 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 77 NSLog(@"其餘狀況,切換主線程處理"); 78 }]; 79 break; 80 } 81 } 82 } 83 }]; 84 }else{ 85 NSLog(@"不支持指紋識別"); 86 switch (error.code) { 87 case LAErrorTouchIDNotEnrolled: 88 { 89 NSLog(@"TouchID is not enrolled"); 90 break; 91 } 92 case LAErrorPasscodeNotSet: 93 { 94 NSLog(@"A passcode has not been set"); 95 break; 96 } 97 default: 98 { 99 NSLog(@"TouchID not available"); 100 break; 101 } 102 } 103 104 NSLog(@"%@",error.localizedDescription); 105 } 106 107 } 108 109 110 111 - (void)didReceiveMemoryWarning { 112 [super didReceiveMemoryWarning]; 113 // Dispose of any resources that can be recreated. 114 } 115 116 117 @end
Swift版本:spa
引入頭文件:線程
import LocalAuthenticationcode
1 // 2 // ViewController.swift 3 // TouchID-Swift 4 // 5 // Created by Shaoting Zhou on 2018/1/23. 6 // Copyright © 2018年 Shaoting Zhou. All rights reserved. 7 // 8 9 import UIKit 10 import LocalAuthentication 11 12 class ViewController: UIViewController { 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 //建立LAContext 17 let context = LAContext.init() 18 19 //這個屬性是設置指紋輸入失敗以後的彈出框的選項 20 context.localizedFallbackTitle = "沒有忘記密碼" 21 22 var error:NSError? = nil 23 if(context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)){ 24 print("支持指紋識別") 25 context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "請按home鍵指紋解鎖", reply: { (success, err) in 26 if(success){ 27 print("驗證成功,刷新界面") 28 }else{ 29 print("發生錯誤:\(String(describing: error))") 30 let error:LAError = err! as! LAError 31 switch (error.code) { 32 case .systemCancel: 33 print("系統取消受權,如其餘APP切入") 34 break 35 case .userCancel: 36 print("用戶取消驗證Touch ID") 37 break 38 case .authenticationFailed: 39 print("受權失敗") 40 break 41 case .userFallback: 42 print("用戶返回") 43 break 44 case .passcodeNotSet: 45 print("用戶還沒有位置指紋") 46 break 47 case .touchIDNotAvailable: 48 print("設備Touch ID未響應") 49 break 50 case .touchIDNotEnrolled: 51 print("設備Touch ID不可用") 52 break 53 case .touchIDLockout: 54 print("ouch ID鎖住") 55 break 56 case .appCancel: 57 print("app驗證Touch ID") 58 break 59 case .invalidContext: 60 print("無效Touch ID") 61 break 62 case .notInteractive: 63 print("不可交互") 64 break 65 } 66 } 67 68 69 }) 70 71 }else{ 72 print("不支持touchID") 73 74 } 75 76 77 } 78 79 80 81 82 83 override func didReceiveMemoryWarning() { 84 super.didReceiveMemoryWarning() 85 // Dispose of any resources that can be recreated. 86 } 87 88 89 }
效果截圖:blog