您的位置:首页 > 移动开发 > IOS开发

iOS 8.0新特性之指纹解锁

2017-03-12 00:30 483 查看
iOS 8.0提供了指纹解锁,使用它我们可以轻松进行当前手机用户指纹的确认。

在使用指纹解锁时需要导入头文件

#import <LocalAuthentication/LocalAuthentication.h>


1、初始化LAContext对象

LAContext *context = [LAContext new];


2、对是否支持指纹解锁进行判定

[context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL]


返回值为当前设备是否支持指纹解锁的状态值

3、支持指纹解锁进行指纹解锁的验证

[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请输入手势密码" reply:^(BOOL success, NSError * _Nullable error) {
//success 为1 表示密码正确
if (success) {
NSLog(@"手势密码成功");
}else{
// Authentication was not successful, because user failed to provide valid credentials.
//LAErrorAuthenticationFailed  指纹无法识别

// Authentication was canceled by user (e.g. tapped Cancel button).
//LAErrorUserCancel            用户点击取消

// Authentication was canceled, because the user tapped the fallback button (Enter Password).
//LAErrorUserFallback          用户点击输入密码

// Authentication was canceled by system (e.g. another application went to foreground).
//LAErrorSystemCancel          系统取消(如电话)

// Authentication could not start, because passcode is not set on the device.
//LAErrorPasscodeNotSet        用户没有设置指纹

// Authentication could not start, because Touch ID is not available on the device.
//LAErrorTouchIDNotAvailable   不能在此设备上使用指纹
NSLog(@"手势密码失败");
}
}];


完整代码:

//iOS 8.0提供的方法
LAContext *context = [LAContext new];
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL]) {
NSLog(@"此设备支持指纹");
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请输入手势密码" reply:^(BOOL success, NSError * _Nullable error) { //success 为1 表示密码正确 if (success) { NSLog(@"手势密码成功"); }else{ // Authentication was not successful, because user failed to provide valid credentials. //LAErrorAuthenticationFailed 指纹无法识别 // Authentication was canceled by user (e.g. tapped Cancel button). //LAErrorUserCancel 用户点击取消 // Authentication was canceled, because the user tapped the fallback button (Enter Password). //LAErrorUserFallback 用户点击输入密码 // Authentication was canceled by system (e.g. another application went to foreground). //LAErrorSystemCancel 系统取消(如电话) // Authentication could not start, because passcode is not set on the device. //LAErrorPasscodeNotSet 用户没有设置指纹 // Authentication could not start, because Touch ID is not available on the device. //LAErrorTouchIDNotAvailable 不能在此设备上使用指纹 NSLog(@"手势密码失败"); } }];
}
else{
NSLog(@"此设备不支持指纹");
}


本人使用模拟器,未能达到理想效果,最好亲自体验。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  指纹解锁