iOS中应用内添加指纹识别的实例代码

时间:2022-06-17 09:35:32

iOS8之后苹果发布了指纹识别的功能,通过touch ID来识别用户,做用户授权,主要是依赖于LocalAuthentication库

指纹识别:一判断设备是否支持指纹识别功能

二识别指纹,成功后做相应的动作,失败后提醒用户指纹识别失败

先引入#import <LocalAuthentication/LocalAuthentication.h>

?
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
LAContext *context = [[LAContext alloc] init];
  NSError *error = nil;
  //验证是否支持指纹识别
  if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
    NSLog(@"支持指纹识别的应用");
    //验证身份
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"需要指纹来验证你的身份", @"hello") reply:^(BOOL success, NSError *error) {
      if (success) {
        NSLog(@"成功");
      } else {
        switch (error.code) {
          case LAErrorUserCancel:
            NSLog(@"用户取消了授权 - %@", error.localizedDescription);
            break;
          case LAErrorUserFallback:
            NSLog(@"用户点击了“输入密码”按钮 - %@", error.localizedDescription);
            break;
          case LAErrorAuthenticationFailed:
            NSLog(@"您已授权失败3次 - %@", error.localizedDescription);
            break;
          case LAErrorTouchIDLockout:
            NSLog(@"指纹被锁定 - %@", error.localizedDescription);
            break;
          case LAErrorSystemCancel:
            NSLog(@"应用程序进入后台 - %@", error.localizedDescription);
            break;
          default:
            NSLog(@"++%@--%zd", error.localizedDescription, error.code);
            break;
        }
      }
    }];
  } else {
    switch (error.code) {
      case LAErrorPasscodeNotSet:
        NSLog(@"未设置密码 - %@", error.localizedDescription);
        break;
      case LAErrorTouchIDNotEnrolled:
        NSLog(@"未注册 Touch ID - %@", error.localizedDescription);
        break;
      case kLAErrorTouchIDNotAvailable:
        NSLog(@"该设备不支持 Touch ID - %@", error.localizedDescription);
        break;
      default:
        NSLog(@"--%@--%zd", error.localizedDescription, error.code);
        break;
    }
  }

原文链接:http://www.cnblogs.com/liyy2015/archive/2016/12/13/6169880.html