iOS指纹验证TouchID应用学习教程

2020-01-18 19:37:58于海丽

 


//指纹验证返回值
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,

 /// Authentication was not successful, because there were too many failed Touch ID attempts and
 /// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating
 /// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.
 LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout,

 /// Authentication was canceled by application (e.g. invalidate was called while
 /// authentication was in progress).
 LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel,

 /// LAContext passed to this call has been previously invalidated.
 LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
} NS_ENUM_AVAILABLE(10_10, 8_0);

以上呢,就是一个简单的demo了,可能有些小问题,到时候需要的话可以自调整。这里附上这个demo的guithub链接看这里看这里,链接在这呢。

第二部分:利用现有的第三方组件实现

这个部分可以好好学习一下。

    在这里呢,我要推荐一个别人写的一个第三方的组件,就是[WJTouchID](https://www.easck.com/>

2:引入头文件
#import "WJTouchID.h"

3:遵守协议
@interface ViewController ()<WJTouchIDDelegate>

4: 创建对象
@property (nonatomic, strong) WJTouchID *touchID;
5:调用


- (void)viewDidLoad {
 [super viewDidLoad];
 //初始化
 WJTouchID *touchid = [[WJTouchID alloc]init];
 [touchid startWJTouchIDWithMessage:WJNotice(@"自定义信息", @"The Custom Message") fallbackTitle:WJNotice(@"", @"Fallback Title") delegate:self];
 self.touchID = touchid;
}

6:实现回调函数


@required

 //TouchID验证成功
- (void)WJTouchIDAuthorizeSuccess;

 //TouchID验证失败
- (void)WJTouchIDAuthorizeFailure;

@optional

 //当前设备不支持指纹识别
- (void)WJTouchIDIsNotSupport;

 //当前软件被挂起取消了授权(如突然来了电话,应用进入前台)
- (void)WJTouchIDAuthorizeErrorAppCancel;

 //取消TouchID验证 (用户点击了取消)
- (void)WJTouchIDAuthorizeErrorUserCancel;

 //在TouchID对话框中点击输入密码按钮
- (void)WJTouchIDAuthorizeErrorUserFallback;

 //在验证的TouchID的过程中被系统取消 例如突然来电话、按了Home键、锁屏...
- (void)WJTouchIDAuthorizeErrorSystemCancel;

 //无法启用TouchID,设备没有设置密码
- (void)WJTouchIDAuthorizeErrorPasscodeNotSet;

 //多次连续使用Touch ID失败,Touch ID被锁,需要用户输入密码解锁
- (void)WJTouchIDAuthorizeErrorTouchIDLockout;

 //当前软件被挂起取消了授权 (授权过程中,LAContext对象被释)
- (void)WJTouchIDAuthorizeErrorInvalidContext;

 //设备没有录入TouchID,无法启用TouchID
- (void)WJTouchIDAuthorizeErrorTouchIDNotEnrolled;

 //该设备的TouchID无效
- (void)WJTouchIDAuthorizeErrorTouchIDNotAvailable;