TouchID

#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(100, 100, 100, 100);
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"Touch ID" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}


-(void)action
{

    LAContext *laContext =[[LAContext alloc]init];
    NSError *error = nil;
    
    if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){
        [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Touch IDTest" reply:^(BOOL success, NSError * _Nullable error) {
            if (success){
                NSLog(@"success to evaluate");
                
            }else if (error){
                NSLog(@"---failed----%@",error.description);
                switch (error.code) {
                    case LAErrorSystemCancel:
                    {
                        NSLog(@"Authentication was cancelled by the system");
                        //切换到其他APP,系统取消验证Touch ID
                        break;
                    }
                    case LAErrorUserCancel:
                    {
                        NSLog(@"Authentication was cancelled by the user");
                        //用户取消验证Touch ID
                        break;
                    }
                    case LAErrorUserFallback:
                    {
                        NSLog(@"User selected to enter custom password");
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //用户选择输入密码,切换主线程处理
                        }];
                        break;
                    }
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //其他情况,切换主线程处理
                        }];
                        break;
                    }
                }
            }
            
        }];
        
    }else{
        
        
        NSLog(@"未调用TouchID=======%@=====",error.description);
        
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


typedef NS_ENUM(NSInteger, LAError)
{    //授权失败
    LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,    
    //用户取消Touch ID授权
    LAErrorUserCancel           = kLAErrorUserCancel,    
    //用户选择输入密码
    LAErrorUserFallback         = kLAErrorUserFallback,    
    //系统取消授权(例如其他APP切入)
    LAErrorSystemCancel         = kLAErrorSystemCancel,    
    //系统未设置密码
    LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,    //设备Touch ID不可用,例如未打开
    LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,    
    //设备Touch ID不可用,用户未录入
    LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE(10_10, 8_0);

注:输入错误时,alert下发会出现输入密码框

你可能感兴趣的:(TouchID)