和使用相册、麦克风等一样,需要获取用户隐私权限: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起作用的条件:
- iPhone X后,且支持脸部识别的设备机型;
- iOS 11.3以上的系统;
- 首次权限弹框用户选择”允许”;
iPhone 5s 至 iPhone 8 这些机型调用代码不变,会默认使用Touch ID验证
如果本文对你有所帮助记得点个赞哈:)
网友评论