iOS开发--touchID指纹识别

从iPhone 5s、iOS7开始,就有指纹识别了,不过指纹识别的API好像是iOS8出来之后才开放的,具体记得不是很清楚了。

最近没事研究了一下指纹识别,不过我看到网上关于这方面的资料都不是很全,所以我就来写一下,对我也可以强化一下记忆,让大家也可以参考参考。

其实这个东西已经封装的很简单了,就一个类,里面有2个方法,2个属性,东西很少。

先直接上代码,然后再在下面解释一下。

.h文件什么都没写,直接.m文件了。

#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad]; 
}


- (IBAction)buttonClick:(UIButton *)sender {
    
    LAContext *context = [[LAContext alloc] init];
//    _context.localizedFallbackTitle = @"用密码登录";
//    _context.maxBiometryFailures = @3;
    NSError *error = nil;
    BOOL isSupport = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    if (isSupport) {
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请通过Home键输入您的手机指纹密码" reply:^(BOOL success, NSError *error) {
            if (success) {
                
                NSLog(@"指纹识别成功");
                
            }else{
                
                NSLog(@"%ld",error.code);
                
                if (error.code == LAErrorAuthenticationFailed) {
                    NSLog(@"授权失败,指纹识别三次都错了。");
                }
                if (error.code == LAErrorUserCancel) {
                    NSLog(@"用户取消");
                }
                if (error.code == LAErrorUserFallback) {
                    NSLog(@"用户使用Fallback(用户用其他登陆方式了)");
                }
                if (error.code == LAErrorSystemCancel) {
                    NSLog(@"系统取消");
                }
                
                
            }
        }];
    }else{
	if (error.code == LAErrorPasscodeNotSet) {
        	NSLog(@"密码未设置");
        }
        if (error.code == LAErrorTouchIDNotAvailable) {
        	NSLog(@"touchID不可用");
        }
        if (error.code == LAErrorTouchIDNotEnrolled) {
        	NSLog(@"touchID未录入");
        }
}}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];}


在使用touchID之前,先要导入
#import <LocalAuthentication/LocalAuthentication.h>
这个类中有2个方法,

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

该方法是用来判断设备是否支付指纹识别;

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

该方法是用来识别指纹的。

按上面的代码写了后,运行,会看到如下效果:


在该图中,“指纹识别”四个字是App的名字,下面的一行字“请通过Home键输入您的手机指纹密码”是方法 - (void)evaluatePolicy:(LAPolicy)policy localizedReason:(NSString *)localizedReason reply:(void(^)(BOOL success, NSError *error))reply 中的localizedReason,这个字符串必须要写,不能为空,不然会崩。

这里面的LAPolicy分为两种:

  • LAPolicyDeviceOwnerAuthenticationWithBiometrics
  • 可以简单的理解为,基于 TouchID 的授权;若 Touch ID 不可用,将会授权失败。在后面我们会详细阐述这种授权方式
  • Touch ID,将在错误输入 5 次后被锁定。锁定后,需要到手机锁屏界面输入密码来解锁

  • LAPolicyDeviceOwnerAuthentication
  • 支持两种授权方式,即:Touch ID 和 输入密码
  • 优先以 Touch ID 方式进行授权,若Touch ID输入错误5次后,会弹出输入密码的界面
  • 输入密码授权,将在错误输入 6 次后被锁定
  • 若 Touch ID 和 输入密码都不用,将授权失败

如果指纹识别成功,那么就会进入if(success){}这个方法;

如果指纹识别不成功,那么就会进入else的方法中,失败的原因有很多。

在LAError中详细列出了这些失败的code,

//
//  LAError.h
//  LocalAuthentication
//
//  Copyright (c) 2013 Apple. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <LocalAuthentication/LAPublicDefines.h>

typedef NS_ENUM(NSInteger, LAError)
{
    /// Authentication was not successful, because user failed to provide valid credentials.
    LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
    
    /// Authentication was canceled by user (e.g. tapped Cancel button).
    LAErrorUserCancel           = kLAErrorUserCancel,
    
    /// Authentication was canceled, because the user tapped the fallback button (Enter Password).
    LAErrorUserFallback         = kLAErrorUserFallback,
    
    /// Authentication was canceled by system (e.g. another application went to foreground).
    LAErrorSystemCancel         = kLAErrorSystemCancel,
    
    /// Authentication could not start, because passcode is not set on the device.
    LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,

    /// Authentication could not start, because Touch ID is not available on the device.
    LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,
    
    /// Authentication could not start, because Touch ID has no enrolled fingers.
    LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);

/// LocalAuthentication error domain.
extern NSString *const LAErrorDomain NS_AVAILABLE(10_10, 8_0);

指纹识别默认是3次机会,如果三次都失败了,那么输入指纹的这个AlertView就会dismiss。

#import <LocalAuthentication/LAContext.h>中有一个属性,

@property (nonatomic)NSNumber *maxBiometryFailuresNS_AVAILABLE(10_10,8_1);

就是用来设置其最大可失败次数的。

如果你指纹识别失败,会出现这个界面:


该界面中,“输入密码”这个按钮上的字样有该类中的另外一个属性所控制,

@property (nonatomic,copy)NSString *localizedFallbackTitle;

对,就是它!默认是Enter Password,输入密码。

如果你点了这个按钮,会进入

if (error.code ==LAErrorUserFallback) {

                    NSLog(@"用户使用Fallback(用户用其他登陆方式了)");

 }

这个方法。

如果你三次指纹识别都失败了,AlertView消息,随后进入

if (error.code ==LAErrorAuthenticationFailed) {

                    NSLog(@"授权失败,指纹识别三次都错了。");

}

该方法,你可以对其进行其他操作。

OK,大致就这些了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值