iOS_组件_TouchID_封装及使用

一丶

指纹识别是iPhone5s iOS8.0之后推出的功能,需要硬件以及软件的支持。

二丶代码

//
//  NSObject+TouchID.h
//  TouchID
//
//  Created by xzb on 2017/4/20.
//  Copyright © 2017年 xzb. All rights reserved.
//

/*
 注意:
 1.前三次指纹验证失败,指纹验证框不再弹出。
 2.再次重新进入验证,还有两次验证机会,如果还是验证失败,TOUCH ID 被锁住不再继续弹出指纹验证框。
 (在iOS10.0以下系统中,不包括iOS 10.0,以后每次进来验证都是调用系统的设备密码直至输入正确的设备密码方可解除TOUCH ID锁,
 但是在iOS 10.0系统中,指纹识别累计错误5次系统判定指纹无效后不会弹出系统的设备密码输入页面)\
 */

#import 
#import 

typedef void(^TouchIDCompleteBlock)(BOOL success, NSError *error);


@interface NSObject (TouchID)


/**
 手机是否可以使用touchid
 1.设备支持,但是没设置 = NO;
 2.设置支持,打开 = YES;
 3.设置不支持 = NO;
 */
-(BOOL)validateCanUseTouchId;

/**
 弹出TouchID指示框;

 @param fallbackTitle 失败的时候,右边title,原来是输入密码;
 @param title title
 @param touchBlock 事件block
 */
-(void)touchIDWithComplete:(TouchIDCompleteBlock) touchBlock;

@end

//
//  NSObject+TouchID.m
//  TouchID
//
//  Created by xzb on 2017/4/20.
//  Copyright © 2017年 xzb. All rights reserved.
//

#import "NSObject+TouchID.h"



@implementation NSObject (TouchID)

#pragma mark - 生命周期


#pragma mark - api
-(BOOL)validateCanUseTouchId
{
    
    return [self validateCanUseTouchIdWithBlock:nil];
}

-(void)touchIDWithComplete:(TouchIDCompleteBlock)touchBlock
{
    [self validateCanUseTouchIdWithBlock:^(BOOL succeed, LAContext *locontext, NSError *error) {
        
        if (succeed)
        {
            LAPolicy policy = LAPolicyDeviceOwnerAuthentication;
            
#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_8_0
            policy = LAPolicyDeviceOwnerAuthenticationWithBiometrics;
#else
            policy= LAPolicyDeviceOwnerAuthentication;
#endif
            //修改文字,原本为'请输入密码'
//           locontext.localizedFallbackTitle= @"123";

            
            [locontext  evaluatePolicy:policy localizedReason:@"通过Home键验证已有手机指纹" reply:^(BOOL success, NSError * error) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                   
                    touchBlock(success,error);
                    NSString *mesgStr = @"";
                    if (success)
                    {
                        mesgStr = @"指纹验证通过";
                    }
                    else if (error.code == kLAErrorUserCancel)
                    {
                        mesgStr = @"用户取消";
                    }
                    else if (error.code == kLAErrorAuthenticationFailed)
                    {
                        mesgStr = @"鉴定失败";
                    }
                    else if (error.code == kLAErrorUserFallback)
                    {
                        mesgStr = @"用户选择输入密码";
                    }
                    else if (error.code == kLAErrorSystemCancel)
                    {
                        mesgStr = @"系统取消(如:另外一个应用进入前台)";
                    }
                    NSLog(@"mesgStr : %@",mesgStr);
                    
                });
            }];
        }
        else
        {
            //不支持指纹识别,LOG出错误详情
            switch (error.code) {
                case LAErrorTouchIDNotEnrolled:
                {
                    NSLog(@"指纹未录入");
                    break;
                }
                case LAErrorPasscodeNotSet:
                {
                    NSLog(@"密码没设置");
                    break;
                }
                case LAErrorTouchIDNotAvailable:
                {
                    NSLog(@"TouchID 不可用");
                    break;
                }
                case LAErrorTouchIDLockout:
                {
                    NSLog(@"TouchID 被锁定");
                    break;
                }
                default:
                {
                    NSLog(@"TouchID 不可用");
                    break;
                }
            }
            touchBlock(succeed,error);
            NSLog(@"设备不支持指纹解锁 %@", error);
        }
    }];
}
#pragma mark - getter / setter

#pragma mark - model event

#pragma mark - view event

#pragma mark - private
-(BOOL)validateCanUseTouchIdWithBlock:(void(^)(BOOL succeed,LAContext *locontext, NSError *error))block
{
    LAContext  *locontext= [[LAContext alloc] init];
    NSError *err;
    BOOL canUse =  [locontext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&err];
    
    if (block)
    {
        block(canUse,locontext,err);
    }
    return canUse;
}
#pragma mark - delegete


@end

下载地址:
https://github.com/k373379320/ZBTouchID

三丶使用

    if ([self validateCanUseTouchId])
    {
        [self touchIDWithComplete:^(BOOL success, NSError *error) {
            NSLog(@"success:%@",success?@"成功":@"失败");
        }];
    }
    else
    {
        NSLog(@"不支持TouchID");
    }

四丶参考资料:

更为详细资料,可以阅读以下内容

https://developer.apple.com/reference/localauthentication?language=objc
http://www.documentcloud.org/documents/1302613-ios-security-guide-sept-2014.html
http://www.jianshu.com/p/67fd93408517
http://www.jianshu.com/p/3ff9d8edae8e

你可能感兴趣的:(iOS_组件_TouchID_封装及使用)