iOS 指纹解锁验证TouchID功能

时间:2022-10-14 11:40:08

1、首先,引入依赖框架 LocalAuthentication.framework

?
1
#import <LocalAuthentication/LocalAuthentication.h>

2、然后,判断系统是否为iOS8及以上

?
1
2
3
4
//iOS8.0后才支持指纹识别接口
  if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
    return;
  }

3、最后,在APP启动时调用以下方法即可完成指纹解锁的全部功能集成

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
- (void)evaluateAuthenticate
{
  //创建LAContext
  LAContext* context = [[LAContext alloc] init];
  NSError* error = nil;
  NSString* result = @"请验证已有指纹";
  //首先使用canEvaluatePolicy 判断设备支持状态
  if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
    //支持指纹验证
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
      if (success) {
        //验证成功,主线程处理UI
      }
      else
      {
        NSLog(@"%@",error.localizedDescription);
        switch (error.code) {
          case LAErrorSystemCancel:
          {
            //系统取消授权,如其他APP切入
            break;
          }
          case LAErrorUserCancel:
          {
            //用户取消验证Touch ID
            break;
          }
          case LAErrorAuthenticationFailed:
          {
            //授权失败
            break;
          }
          case LAErrorPasscodeNotSet:
          {
            //系统未设置密码
            break;
          }
          case LAErrorTouchIDNotAvailable:
          {
            //设备Touch ID不可用,例如未打开
            break;
          }
          case LAErrorTouchIDNotEnrolled:
          {
            //设备Touch ID不可用,用户未录入
            break;
          }
          case LAErrorUserFallback:
          {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
              //用户选择输入密码,切换主线程处理
            }];
            break;
          }
          default:
          {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
              //其他情况,切换主线程处理
            }];
            break;
          }
        }
      }
    }];
  }
  else
  {
    //不支持指纹识别,LOG出错误详情
    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);
  }
}

以上所述是小编给大家介绍的iOS 指纹解锁验证TouchID功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/jukaiit/p/6438818.html