您的位置:首页 > 其它

LocalAuthentication

2016-06-26 14:05 295 查看
#import "TouchViewController.h"
//使用指纹验证必须要使用LocalAuthentication类库来支持
//LocalAuthentication类库包含两个分类,一个是LAContext,一个是error
//LAContext这个类是专门来创建与使用指纹验证的
//注:指纹验证,他只是通过我们指纹触发的一个事件,并不是非得用于登录、支付等功能
#import <LocalAuthentication/LocalAuthentication.h>
@interface TouchViewController ()

@end

@implementation TouchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建一个button来触发我们指纹验证的操作
    UIButton *touchBT = [UIButton buttonWithType:UIButtonTypeSystem];
    [touchBT setTitle:@"指纹验证" forState:UIControlStateNormal];
    touchBT.frame = CGRectMake(100,
100, 100,
100);
    [touchBT addTarget:self action:@selector(touchForPassWord) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:touchBT];
}

- (void)touchForPassWord
{
    //LAContext首次出现是在8.0以后
    //关于这个类的介绍,官方给出的意思大致为:该类为开发者提供了一套指纹和密码的方案来供我们使用
    LAContext *myContext = [[LAContext alloc]init];
    //错误信息是必不可少的,你验证失败、无法使用等等操作都需要我们来做
    NSError *error = nil;
    //这个字符串作为我们验证时的提示信息
    NSString *myLocalizedReasonString = @"请输入指纹";
    //这里我们需要做一个判断,因为你判断iOS5、6、7、8是没有办法完全辨别出来你的设备是否能够使用指纹验证的,现在我们的LAContext直接给了我们一个方法是可以判断我们的当前设备是否支持使用指纹验证
    //验证的方法当中有两个参数是让我们去填写的
    //第一个参数的类型是LAPolicy,这个类型的参数只有一个(枚举值),其实通过单词的音译我们就可以判断出来该参数是用作于我们可以使用指纹验证的
    //他的内部会进行一个验证,如果是未能启用touchID,那将走一个失败的方法
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        //如果可以使用指纹识别我们将调用验证指纹的方法
        //evaluatePolicy的意思为验证
        //参数依然为我们的LAPolicyDeviceOwnerAuthenticationWithBiometrics
        //localizedReason里需要一个我们的提示信息
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError *error) {
            //如果验证成功,也就是指纹验证成功后,将走下面这个方法,具体实现什么内容由你来决定
            if (success) {
                NSLog(@"验证成功");
                
            }else//验证失败就走其他的方法咯~
            {
                //error信息也是给我们提供了验证的参数的~
                switch (error.code) {
                    case kLAErrorUserFallback:
                        NSLog(@"用户选择输入密码~~~");
                        break;
                    case kLAErrorUserCancel:
                        NSLog(@"用户取消了操作");
                        break;
                    default:
                        NSLog(@"身份验证失败,呵呵哒~");
                        break;
                }
            }
        }];
    }
    else
    {
        //没有touchID的小伙伴们赶紧去刷6S吧,坐等
        NSLog(@"无法使用touchID,%@",error);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: