当前位置: 移动技术网 > 移动技术>移动开发>IOS > 浅析IOS开发TouchID指纹解锁功能

浅析IOS开发TouchID指纹解锁功能

2019年07月24日  | 移动技术网移动技术  | 我要评论

效果图如下

实现过程

1.首先引入指纹解锁必须的头文件

#import <localauthentication/localauthentication.h>

2.了解下主要的两个方法
这个方法是判断设备是否支持touchid的。

- (bool)canevaluatepolicy:(lapolicy)policy error:(nserror * __autoreleasing *)error __attribute__((swift_error(none)));

这个是用来验证touchid的,会有弹出框出来。

- (void)evaluatepolicy:(lapolicy)policy
  localizedreason:(nsstring *)localizedreason
     reply:(void(^)(bool success, nserror * __nullable error))reply;

3.新建lacontext对象

主要的属性设置

localizedfallbacktitle:用于设置左边的按钮的名称,默认是enter password.

localizedreason:用于设置提示语,表示为什么要使用touch id


解锁失败界面

 //创建lacontext
 lacontext *context = [lacontext new];

 
 //这个属性是设置指纹输入失败之后的弹出框的选项
 context.localizedfallbacktitle = @"没有忘记密码";

4.主要回调方法,包括成功以及失败的

nserror *error = nil;
 if ([context canevaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics error:&error]) {
  nslog(@"支持指纹识别");
  [context evaluatepolicy:lapolicydeviceownerauthenticationwithbiometrics localizedreason:@"指纹解锁" reply:^(bool success, nserror * _nullable error) {
   if (success) {
    nslog(@"验证成功 刷新主界面");
   }else{
    nslog(@"%@",error.localizeddescription);
    switch (error.code) {
     case laerrorsystemcancel:
     {
      nslog(@"系统取消授权,如其他app切入");
      break;
     }
     case laerrorusercancel:
     {
      nslog(@"用户取消验证touch id");
      break;
     }
     case laerrorauthenticationfailed:
     {
      nslog(@"授权失败");
      break;
     }
     case laerrorpasscodenotset:
     {
      nslog(@"系统未设置密码");
      break;
     }
     case laerrortouchidnotavailable:
     {
      nslog(@"设备touch id不可用,例如未打开");
      break;
     }
     case laerrortouchidnotenrolled:
     {
      nslog(@"设备touch id不可用,用户未录入");
      break;
     }
     case laerroruserfallback:
     {
      [[nsoperationqueue mainqueue] addoperationwithblock:^{
       nslog(@"用户选择输入密码,切换主线程处理");
      }];
      break;
     }
     default:
     {
      [[nsoperationqueue mainqueue] addoperationwithblock:^{
       nslog(@"其他情况,切换主线程处理");
      }];
      break;
     }
    }
   }
  }];
 }else{
  nslog(@"不支持指纹识别");
  switch (error.code) {
   case laerrortouchidnotenrolled:
   {
    nslog(@"touchid is not enrolled");
    break;
   }
   case laerrorpasscodenotset:
   {
    nslog(@"a passcode has not been set");
    break;
   }
   default:
   {
    nslog(@"touchid not available");
    break;
   }
  }

  nslog(@"%@",error.localizeddescription);
 }

总结

到这里指纹解锁功能几乎就算完成,使用确实很简单,因为苹果都已经给我们做好一切,对我们开发者来说就很轻松了。教程写的很简陋,希望大家多多包涵,如果有疑问大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网