指纹识别

指纹识别: 从iPhone5s开始,苹果手机有了指纹识别功能,而从iOS8.0开始,苹果开放了指纹识别的接口。

简单实现

导入#import

#import 
核心代码
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    //判断系统版本
    /*
     iPhone5s开始有了指纹识别功能
     iOS8开始苹果开放指纹识别接口
     */
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        
        //判断是否可用指纹识别功能
        
        //创建上下文
        LAContext *context = [[LAContext alloc] init];
        
        //指纹验证失败后 右侧按钮显示文字
        context.localizedFallbackTitle = @"验证登录密码";
        
        //判断是否可用
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
            //可用
            
            //启动指纹识别功能
            //localizedReason: 显示在界面上的原因
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"通过Home键验证已有手机指纹" reply:^(BOOL success, NSError * _Nullable error) {
                
                //判断是否成功
                if (success) {
                    NSLog(@"验证成功");
                }else {
                    NSLog(@"验证失败");
                }
                
                //如果区分不同错误来提示用户 必须判断error
                if (error) {
                    NSLog(@"error: %@-----code: %ld-----userInfo: %@", error, error.code, error.userInfo);
                }
                
                /*
                 typedef NS_ENUM(NSInteger, LAError)
                 {
                 /// Authentication was not successful, because user failed to provide valid credentials.
                 LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
                 
                 /// Authentication was canceled by user (e.g. tapped Cancel button).
                 LAErrorUserCancel           = kLAErrorUserCancel,
                 
                 /// Authentication was canceled, because the user tapped the fallback button (Enter Password).
                 LAErrorUserFallback         = kLAErrorUserFallback,
                 
                 /// Authentication was canceled by system (e.g. another application went to foreground).
                 LAErrorSystemCancel         = kLAErrorSystemCancel,
                 
                 /// Authentication could not start, because passcode is not set on the device.
                 LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,
                 
                 /// Authentication could not start, because Touch ID is not available on the device.
                 LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,
                 
                 /// Authentication could not start, because Touch ID has no enrolled fingers.
                 LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
                 
                 /// Authentication was not successful, because there were too many failed Touch ID attempts and
                 /// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating
                 /// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.
                 LAErrorTouchIDLockout   NS_ENUM_AVAILABLE(10_11, 9_0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0) = kLAErrorTouchIDLockout,
                 
                 /// Authentication was canceled by application (e.g. invalidate was called while
                 /// authentication was in progress).
                 LAErrorAppCancel        NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel,
                 
                 /// LAContext passed to this call has been previously invalidated.
                 LAErrorInvalidContext   NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
                 } NS_ENUM_AVAILABLE(10_10, 8_0) __WATCHOS_AVAILABLE(3.0) __TVOS_AVAILABLE(10.0);
                 */
                
            }];
            
        }else {
            //不可以使用
            NSLog(@"iPhone5s以上机型才可使用指纹识别功能");
        }
        
    }else {
        NSLog(@"系统版本过低!");
    }
    
}
IMG_3252.jpg

点击取消按钮输出结果:

[ViewController.m:52行] 验证失败
[ViewController.m:57行] error: Error Domain=com.apple.LocalAuthentication Code=-2 "Canceled by user." UserInfo={NSLocalizedDescription=Canceled by user.}-----code: -2-----userInfo: {
    NSLocalizedDescription = "Canceled by user.";
}

故意用错误指纹,第三次错误时输出:

[ViewController.m:52行] 验证失败
[ViewController.m:57行] error: Error Domain=com.apple.LocalAuthentication Code=-1 "Application retry limit exceeded." UserInfo={NSLocalizedDescription=Application retry limit exceeded.}-----code: -1-----userInfo: {
    NSLocalizedDescription = "Application retry limit exceeded.";
}

PS: 我用的5s测试的,奇怪的是竟然遇到几次走到了设备不可用判断里(NSLog(@"iPhone5s以上机型才可使用指纹识别功能");),可是确定手机是货真价实的iPhone5s,支付宝等的指纹识别都可以正常使用。不知道原因,很是无语,不过我用的数据线不是苹果原装的,难道和这个有关系?不解

指纹识别

你可能感兴趣的:(指纹识别)