http://blog.csdn.net/hongfengkt/article/details/49868073
前一阵子一直在赶项目进度,没有太多时间写博客,现在终于空闲了,将以前欠下的博客补上来。
在iOS8.0之后,API开放了指纹验证的功能。开放的API虽然只能用来作为验证使用,比较局限,但是对于有类似“手势密码”功能的app来说,已经足够用了。有了这个新api,就可以在app中再多一种高大上的解锁方式了。
指纹验证功能的最低硬件支持为iPhone5s,iPad 6,iPad mini 3这些有touch ID硬件支持的设备,并且操作系统最低为iOS8.0,因为touch ID是在iOS8.0之后才开放的一类api。
下面放代码。
做iOS8.0下版本适配时,务必进行API验证,避免调用相关API引起崩溃。
引入依赖框架:
#import <LocalAuthentication/LocalAuthentication.h>
- 1
- 1
指纹验证的实现
- (void)authenticateUser
{
//初始化上下文对象
LAContext* context = [[LAContext alloc] init];
//错误对象
NSError* error = nil;
NSString* result = @"Authentication is needed to access your notes.";
//首先使用canEvaluatePolicy 判断设备支持状态
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
//支持指纹验证
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
if (success) {
//验证成功,主线程处理UI
}
else
{
NSLog(@"%@",error.localizedDescription);
switch (error.code) {
case LAErrorSystemCancel:
{
NSLog(@"Authentication was cancelled by the system");
//切换到其他APP,系统取消验证Touch ID
break;
}
case LAErrorUserCancel:
{
NSLog(@"Authentication was cancelled by the user");
//用户取消验证Touch ID
break;
}
case LAErrorUserFallback:
{
NSLog(@"User selected to enter custom password");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//用户选择其他验证方式,切换主线程处理
}];
break;
}
default:
{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//其他情况,切换主线程处理
}];
break;
}
}
}
}];
}
else
{
//不支持指纹识别,LOG出错误详情
switch (error.code) {
case LAErrorTouchIDNotEnrolled:
{
NSLog(@"TouchID is not enrolled");
break;
}
case LAErrorPasscodeNotSet:
{
NSLog(@"A passcode has not been set");
break;
}
default:
{
NSLog(@"TouchID not available");
break;
}
}
NSLog(@"%@",error.localizedDescription);
[self showPasswordAlert];
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
对几种情况的说明
typedef NS_ENUM(NSInteger, LAError)
{
//授权失败
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
//用户取消Touch ID授权
LAErrorUserCancel = kLAErrorUserCancel,
//用户选择输入密码
LAErrorUserFallback = kLAErrorUserFallback,
//系统取消授权(例如其他APP切入)
LAErrorSystemCancel = kLAErrorSystemCancel,
//系统未设置密码
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
//设备Touch ID不可用,例如未打开
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
//设备Touch ID不可用,用户未录入
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);