? ? ? 前段時(shí)間公司開發(fā)需要指紋\人臉識(shí)別功能,指紋識(shí)別和面部識(shí)別公用一個(gè)庫即可一疯,系統(tǒng)會(huì)自動(dòng)判斷當(dāng)前設(shè)備支持指紋識(shí)別還是面部識(shí)別撼玄。
1.導(dǎo)入框架
#import <LocalAuthentication/LocalAuthentication.h>
2.判斷當(dāng)前手機(jī)是否支持指紋解鎖或者人臉識(shí)別功能
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0 ) {
//?支持手勢(shì)密碼或者人臉識(shí)別功能
// 判斷當(dāng)前手機(jī)只吃人臉識(shí)別還是指紋解鎖
if (@available(iOS 11.0, *)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (self.context.biometryType == LABiometryTypeTouchID) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????????//當(dāng)前支持指紋密碼 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}else if (self.context.biometryType == LABiometryTypeFaceID){ ? ? ? ? ? ? ? ? ? ? //當(dāng)前支持指紋密碼 @"人臉識(shí)別"); ? ? ? ? ? ? ? ? ? ? ????????????????????????????????????????????????????? ? } ? else {????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????//當(dāng)前支持指紋密碼指紋密碼";
? ? }? ? ? ?
? ? ?}else{
?// 不支持手勢(shì)密碼或者人臉識(shí)別功能
? }
3.編寫代碼實(shí)現(xiàn)人臉識(shí)別功能
(1).聲明一個(gè)LAContext屬性,LAContext需要強(qiáng)支持有墩邀,否則會(huì)失效哦掌猛。
@property (strong,nonatomic) LAContext* context;
(2).可以用懶加載去初始化LAContext,但是這樣子的話兩次驗(yàn)證需要時(shí)間眉睹,除非是重新啟動(dòng)荔茬,不然就得等最短時(shí)間到了才會(huì)重新彈框驗(yàn)證,另一種方式是每次都初始化該屬性竹海,這樣就可以每次都有彈框驗(yàn)證了慕蔚。
方法一:懶加載LAContext屬性
- (LAContext*)context{
? ? if(!_context){
? ? ? ? _context= [[LAContextalloc]init];;
? ? }
? ? return _context;
}
方法二:
self.context = [[LAContext alloc] init];
(3).可以設(shè)置用戶輸入一次錯(cuò)誤之后現(xiàn)實(shí)的文字。
self.context.localizedFallbackTitle = @"使用密碼";
(4)驗(yàn)證用戶是否開啟了指紋解鎖或者人臉識(shí)別
?NSError* error =nil;
? ? if ([_context canEvaluatePolicy:1 error:&error]) {
? ? ? ? if(!error && !config) {
? ? ? ? ? ? [self touchId:_context andLockOut:NO];
? ? ? ? }
? ? ? ? returnYES;
? ? }else{
? ? ? ? switch(error.code) {
? ? ? ? ? ? case LAErrorPasscodeNotSet:{
?? ? ? ? ? ? ? NSLog(@"認(rèn)證無法啟動(dòng)斋配,因?yàn)樵O(shè)備沒有設(shè)置密碼孔飒。");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? case LAErrorTouchIDNotEnrolled:{
//? ? ? ? ? 你尚未設(shè)置Touch ID,請(qǐng)?jiān)谑謾C(jī)系統(tǒng)“設(shè)置>Touch ID與密碼“中添加指紋
? ? ? ? ? ? ? ?break;
? ? ? ? ? ? }
? ? ? ? ? ? case LAErrorTouchIDLockout:{
// ?用戶開啟了指紋解鎖或者人臉識(shí)別
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? default:{
? ? ? ? ? ? ? ? NSLog(@"TouchID? 不可用");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
(5).正式開始驗(yàn)證指紋解鎖或者人臉識(shí)別是否正確
//? ? LAPolicyDeviceOwnerAuthenticationWithBiometrics 艰争,指紋授權(quán)使用坏瞄, 當(dāng)設(shè)備不具有Touch ID的功能,或者在系統(tǒng)設(shè)置中沒有設(shè)置開啟指紋甩卓,授權(quán)將會(huì)失敗鸠匀。當(dāng)指紋驗(yàn)證3+2次都沒有通過的時(shí)候指紋驗(yàn)證就會(huì)被鎖定,就需要先進(jìn)行數(shù)字密碼的解鎖才能繼續(xù)使用指紋密碼逾柿。 //? ? LAPolicyDeviceOwnerAuthentication缀棍,指紋和數(shù)字密碼的授權(quán)使用,當(dāng)指紋可用且沒有被鎖定机错,授權(quán)后會(huì)進(jìn)入指紋密碼驗(yàn)證睦柴。不然的話會(huì)進(jìn)入數(shù)字密碼驗(yàn)證的頁面。當(dāng)系統(tǒng)數(shù)字密碼沒有設(shè)置不可用的時(shí)候毡熏,授權(quán)失敗坦敌。如果數(shù)字密碼輸入不正確,連續(xù)6次輸入數(shù)字密碼都不正確后,會(huì)停用鑒定過一定的間隔后才能使用狱窘,間隔時(shí)間依次增長杜顺。
這里通過傳遞一個(gè)BOOL值判斷使用哪個(gè)
?NSInteger lopli = lock?2:1; ? ????????????????????????????????????????????????????????????????????????????? NSString * result_cn = !lock?@"請(qǐng)驗(yàn)證已有指紋":@"錯(cuò)誤次數(shù)過多了,需要密碼以啟用TouchID";?
?[contxt ?evaluatePolicy:lopli ? localizedReason:result_cn ? reply:^(BOOLsuccess,NSError*error) {
? ? ? ? if(success) {
? ? ? ? ? ? //驗(yàn)證成功,主線程處理UI
? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? NSLog(@"驗(yàn)證成功");
? ? ? ? ? ? });
? ? ? ? }else{
? ? ? ? ? ? switch(error.code) {
? ? ? ? ? ? ? ? caseLAErrorSystemCancel:{
? ? ? ? ? ? ? ? ? ? //系統(tǒng)取消授權(quán)蘸炸,如其他APP切入
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? caseLAErrorUserCancel:{
? ? ? ? ? ? ? ? ? ? //用戶取消驗(yàn)證Touch ID
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case LAErrorAuthenticationFailed:{
? ? ? ? ? ? ? ? ? ? //授權(quán)失敗
? ? ? ? ? ? ? ? ? ? if ([error.localizedDescription isEqualToString:@"Application retry limit exceeded."]){
? ? ? ? ? ? ? ? ? ? ? ? [selftouchId:contxtandLockOut:NO];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case LAErrorPasscodeNotSet:{
? ? ? ? ? ? ? ? ? ? //系統(tǒng)未設(shè)置密碼
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case LAErrorTouchIDNotAvailable:{
? ? ? ? ? ? ? ? ? ? //設(shè)備Touch ID不可用躬络,例如未打開
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? caseLAErrorUserFallback:{
? ? ? ? ? ? ? ? ? ? // 多次驗(yàn)證,指紋或者人臉已失效
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? case LAErrorTouchIDLockout:{
? ? ? ? ? ? ? ? ? ? if ([error.localizedDescription isEqualToString:@"Biometry is disabled for unlock."]){
? ? ? ? ? ? ? ? ? ? ? ? [selftouchId:contxtandLockOut:NO];
? ? ? ? ? ? ? ? ? ? }elseif([error.localizedDescriptionisEqualToString:@"Biometry is locked out."]){
? ? ? ? ? ? ? ? ? ? ? ? [selftouchId:contxtandLockOut:YES];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? caseLAErrorAppCancel:{
? ? ? ? ? ? ? ? ? ? //應(yīng)用程序取消了身份驗(yàn)證(例如搭儒,調(diào)用了無效)穷当。(正在進(jìn)行身份驗(yàn)證)。
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? default:{
? ? ? ? ? ? ? ? ? ? [[NSOperationQueue mainQueue] addOperationWithBlock:^{
? ? ? ? ? ? ? ? ? ? ? ? //其他情況淹禾,切換主線程處理
? ? ? ? ? ? ? ? ? ? }];
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }];
相關(guān)代碼地址:git地址