美文网首页
iOS 应用内验证Touch ID、Face ID

iOS 应用内验证Touch ID、Face ID

作者: Geniune | 来源:发表于2021-02-20 15:28 被阅读0次

和使用相册、麦克风等一样,需要获取用户隐私权限:Privacy - Face ID Usage Description
在info.plist中添加:

NSFaceIDUsageDescription

#import <LocalAuthentication/LAContext.h>

- (void)authenticateAction:(id)sender{
    
    if(@available(iOS 8.0, *)){

        LAContext *context = [[LAContext alloc] init];
        
        NSError *error;
        
        if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]){
            
            NSString *reason = @"Log in to your account";
            
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:reason reply:^(BOOL success, NSError * _Nullable error) {
                
                if(success){
                    
                    NSLog(@"Authenticate success");
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //注意:如果需要刷新UI,请把代码写在这里
                    });
                }else{

                    NSLog(@"Authenticate fail ,reason: %@", error.localizedDescription);
                }
            }];
        }
    };
}

Face ID起作用的条件:

  1. iPhone X后,且支持脸部识别的设备机型;
  2. iOS 11.3以上的系统;
  3. 首次权限弹框用户选择”允许”;

iPhone 5s 至 iPhone 8 这些机型调用代码不变,会默认使用Touch ID验证

如果本文对你有所帮助记得点个赞哈:)

相关文章

网友评论

      本文标题:iOS 应用内验证Touch ID、Face ID

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