美文网首页
iOS Touch ID开发记录

iOS Touch ID开发记录

作者: lance017 | 来源:发表于2016-05-26 12:57 被阅读88次

由于最近的项目中用到了Touch ID,这里就给大家介绍下用法

1)首先你需要导入LocalAuthentication.framework,如下图所示

Touch_ID

2)在需要使用的文件导入#import <LocalAuthentication/LocalAuthentication.h>

3)因为TouchID只适用于iOS8及以上,所以我们首先需要判断系统是否为iOS8及以上SystemVersion [UIDevice currentDevice].systemVersion.doubleValue >= 8

然后判断机型是否支持TouchID,判断的方法为

- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError *__autoreleasing *)error;

然后直接调用以下的方法直接调用TouchID

- (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply;
 

[Demo示例][1]
[1]: https://github.com/lance017/TouchIDDemo

具体的代码在这
#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

#define SystemVersion [UIDevice currentDevice].systemVersion.doubleValue

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [[UIButton alloc]init];
    button.center = self.view.center;
    button.bounds = CGRectMake(0, 0, 100, 40);
    [button setTitle:@"指纹识别" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)click {
    //只有iOS8以上支持Touch ID
    if (SystemVersion >= 8.0) {
        LAContext *context = [[LAContext alloc]init];
        NSError *error = nil;
        //验证机器是否支持指纹识别
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            //支持指纹识别
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"APP请求验证" reply:^(BOOL success, NSError * _Nullable error) {
                if (success) {
                    NSLog(@"验证成功");
                }
                
                switch (error.code) {
                    case LAErrorAppCancel:
                        NSLog(@"切换到其他APP,系统取消验证");
                        break;
                    case LAErrorUserCancel:
                        NSLog(@"用户取消验证");
                        break;
                    case LAErrorUserFallback:
                        NSLog(@"用户选择输入密码,切换主线程处理");
                        break;
                    default:
                        break;
                }
                
                if (error) {
                    NSLog(@"%@",error.localizedDescription);
                }
                
            }];
        }else{
            //机器不支持指纹识别
        }
    }else{
        //iOS8以下不支持Touch ID
    }
}
@end

相关文章

  • iOS Touch ID开发记录

    由于最近的项目中用到了Touch ID,这里就给大家介绍下用法 1)首先你需要导入LocalAuthenticat...

  • iOS Touch ID 开发

    Touch ID Touch ID是iPhone5S后加入的一项新的功能,也就是大家熟知的指纹识别技术。大家用得最...

  • iOS Touch ID 开发教程

    一、Touch ID 简介 iOS系统的指纹识别功能最低支持的机型为iPhone 5s,最低支持系统为iOS 8,...

  • 指纹解锁

    参考博客: iOS Touch ID 简易开发教程 上述文章已经说的很清楚了 自己在这里稍微记录一下 一:支持系统...

  • iOS face ID 与 Touch ID 开发

    一、Touch ID let context = LAContext.init()var error:NSErro...

  • 实现TouchID指纹解锁(图文并茂)

    简介 iOS8.0之后Touch ID的API开发给开发,最低支持的机型为iPhone 5s,最低支持系统为ios...

  • iOS Touch ID

    iOS 8 SDK向开发者公开了Touch ID指纹识别功能,允许App对用户身份进行本地验证。使用Touch I...

  • app高大上的指纹识别登陆

    iOS 8 SDK向开发者公开了Touch ID指纹识别功能,允许App对用户身份进行本地验证。使用Touch I...

  • Touch ID 开发

    Touch ID Touch ID是iPhone5S后加入的一项新的功能,也就是大家熟知的指纹识别技术。大家用得最...

  • Touch ID 开发

    .deviceOwnerAuthenticationWithBiometrics: 指纹识别失败后, 点击输入密...

网友评论

      本文标题:iOS Touch ID开发记录

      本文链接:https://www.haomeiwen.com/subject/eccxdttx.html