装逼特技--touchID
2016-02-15  本文已影响195人 
改变自己_now
Touch ID指纹识别作为iPhone 5s上的“杀手级”功能早已为人们所熟知,iPhone 6系列、iPhone 6s系列、iPad Pro、iPad mini 4、iPad mini 3和iPad air 2也使用了Touch ID。[1] 苹果把用户的指纹数据存放在处理器的安全区域(Secure Enclave)中,充分保护用户的数据安全。除此之外,苹果还有另外一道指纹数据安全防线,以一种前所未有的硬件技术实现了对用户数据的保护。因此只有5s以上的设备和iOS8以上的系统的才支持touchID。
正题:
废话不多说,直接开搞。
首先
#import <LocalAuthentication/LocalAuthentication.h>
然后判断系统是否大于等于8.0
为了方便使用我们写一个宏
  #define iOS8  ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (!iOS8) {
    // 如果低于8.0直接返回
    return;
}
//调用touchID
[self toucheIDShow];
}
调用方法实现如下:
- (void)toucheIDShow {
// 创建指纹验证对象
LAContext *context = [[LAContext alloc] init];
NSError *error = nil;
// 验证其是否支持touchID
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
    
    // 如果支持的话就开启touchID
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"我就是弄这玩" reply:^(BOOL success, NSError * _Nullable error) {
       
        
        if (success) {
            
            // 验证成功,在这里做验证成功的操作
            NSLog(@"验证成功");
            
        }
        else {
        
            NSLog(@"errorCode--%d,error%@",error.code,error.localizedDescription);
        }
        
        
    }];
    
}
else {
    // 不支持touchID打印错误
    if (error) {
        
        NSLog(@"%@",error.localizedDescription);
    }
}
}
用模拟器运行的效果
settings.gif
注意:模拟器的设置如下图,要不会显示不支持touchID
hard.png
就是这么简单轻松!!!