仿支付宝指纹解锁

TouchID.gif

自从iPhone5s首次加入TouchID之后,LocalAuthentication也越来越受到关注。

LocalAuthentication 是iOS指纹识别的重要框架。LocalAuthentication以LAContext的方式工作,先用canEvaluatePolicy:error:方法判断机器是否具有指纹识别的功能,再用evaluatePolicy:localizedReason:reply:方法来实现指纹识别功能。整个过程中,用户的生物信息都被安全的存储在硬件当中。

1.判断是否是真机,我们知道模拟机是无法录入指纹的

//To determine whether the device is a simulator
- (BOOL)isSimulator {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceMachine = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([deviceMachine isEqualToString:@"i386"] || [deviceMachine isEqualToString:@"x86_64"]) {
        return YES;
    }
    return NO;
}

2.录入指纹

- (IBAction)touchID:(id)sender {
    if (self.isSimulator) {
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"请用真机测试~" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil] show];
        return;
    }
    //it is the touch id
    LAContext *laContext = [LAContext new];
    NSError *error = nil;
    //To determine whether the device have touch ID 
    if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
        [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"请验证已有指纹" reply:^(BOOL success, NSError * _Nullable error) {
            if (error) {
                NSLog(@"验证失败");//the system will auto to tip
            }else{
                [self presentViewController:[SuccessViewController new] animated:YES completion:nil];
            }
        }];
    }else{
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"不支持Touch ID~" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil] show];
    }
}

这里注意一点,当我们指纹验证失败的时候那个弹框系统自动会弹出来提示,不用我们再写了。。

你可能感兴趣的:(仿支付宝指纹解锁)