美文网首页
iOS touchID的使用(指纹识别代替密码)

iOS touchID的使用(指纹识别代替密码)

作者: 辛乐 | 来源:发表于2016-09-19 23:43 被阅读183次
    #import <LocalAuthentication/LocalAuthentication.h>
    
    //指纹识别的操作
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        //1. 判断系统版本
        if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
            
            //2. LAContext : 本地验证对象上下文
            LAContext *context = [LAContext new];
            
            //3. 判断是否可用
            //Evaluate: 评估  Policy: 策略,方针
            //LAPolicyDeviceOwnerAuthenticationWithBiometrics: 允许设备拥有者使用生物识别技术
            if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"对不起, 指纹识别技术暂时不可用" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                    [alertView show];
                });
                
            }
            
            //4. 开始使用指纹识别
            //localizedReason: 指纹识别出现时的提示文字, 一般填写为什么使用指纹识别
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"开启了指纹识别, 将打开隐藏功能" reply:^(BOOL success, NSError * _Nullable error) {
                
                if (success) {
                    NSLog(@"指纹识别成功");
                    // 指纹识别成功,回主线程更新UI,弹出提示框
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"指纹识别成功" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                        [alertView show];
                    });
                    
                }
                
                if (error) {
                    
                    // 错误的判断chuli
                    
                    if (error.code == -2) {
                        
                        // 取消操作,回主线程更新UI,弹出提示框
                        dispatch_async(dispatch_get_main_queue(), ^{
                            
                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"用户取消了操作" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                            [alertView show];
                            
                        });
                        
                    } else {
                        NSLog(@"错误: %@",error);
                        // 指纹识别出现错误,回主线程更新UI,弹出提示框
                        dispatch_async(dispatch_get_main_queue(), ^{
                            
                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"指纹验证失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                            [alertView show];
                            
                        });
                        
                    }
                    
                }
                
            }];
        } else {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"对不起, 该手机不支持指纹识别" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alertView show];
            });
            
        }
    }
    
    

    相关文章

      网友评论

          本文标题:iOS touchID的使用(指纹识别代替密码)

          本文链接:https://www.haomeiwen.com/subject/ncgnettx.html