Touch ID解锁

1>>>导入库#import

2>>>viewDidLoad中做初始化对象和验证是否支持Touch ID解锁的操作:

LAContext* context = [[LAContext alloc] init];

NSError* error = nil;

/*ios 8.0以上才支持

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

}

或者下面*/

if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {

[self doSomeAuthWith:context];

}

else {//不支持touch ID解锁

NSLog(@"==========Not support :%@", error.description);

}

3>>>自定义的方法doSomeAuthWith:(LAContext *)someThing方法内部做进一步操作和判定

- (void)doSomeAuthWith:(LAContext *)someThing

{

LAContext * myContext = (LAContext *)someThing;

myContext.localizedFallbackTitle = @"输入密码";

NSError *authError = nil;

NSString *myLocalizedReasonString = @"用于解除系统锁定!";

if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError])

{

[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics

localizedReason:myLocalizedReasonString

reply:^(BOOL success, NSError *error) {

if(success)

{

//处理验证通过

NSLog(@"指纹验证通过");

}

else

{

//处理验证失败

NSLog(@"%@",[self getAuthErrorDescription:error.code] );

}

}];

}

else

{

//不支持Touch ID验证,提示用户

}

}

4>>>方法getAuthErrorDescription:(NSInteger)code通过传入错误码返回具体失败原因

- (NSString *)getAuthErrorDescription:(NSInteger)code

{

NSString *msg = @"";

switch (code) {

case LAErrorTouchIDNotEnrolled:

//认证不能开始,因为touch id没有录入指纹.

msg = @"此设备未录入指纹信息!";

break;

case LAErrorTouchIDNotAvailable:

//认证不能开始,因为touch id在此台设备尚是无效的.

msg = @"此设备不支持Touch ID!";

break;

case LAErrorPasscodeNotSet:

//认证不能开始,因为此台设备没有设置密码.

msg = @"未设置密码,无法开启认证!";

break;

case LAErrorSystemCancel:

//认证被系统取消了,例如其他的应用程序到前台了

msg = @"系统取消认证";

break;

case LAErrorUserFallback:

//认证被取消,因为用户点击了fallback按钮(输入密码).

msg = @"选择输入密码!";

break;

case LAErrorUserCancel:

//认证被用户取消,例如点击了cancel按钮.

msg = @"取消认证!";

break;

case LAErrorAuthenticationFailed:

//认证没有成功,因为用户没有成功的提供一个有效的认证资格

msg = @"认证失败!";

break;

default:

break;

}

return msg;

}

你可能感兴趣的:(Touch ID解锁)