如果设备支持Face ID 或Touch ID,我想对用户采取不同的操作。
当使用 Face ID 时,iOS 请求使用许可。 (与 Touch ID 不同)。
如果用户拒绝许可,context.biometryType 返回 LABiometryTypeNone。
有没有办法检查设备支持的Touch ID或Face ID。
LAContext *context = [[LAContext alloc] init];
NSError *error;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
}
if (@available(iOS 11.0, *)) {
if (context.biometryType == LABiometryTypeFaceID) {
// support FaceID
}
}
// support TouchID
控制台输出
(lldb) po error
Error Domain=com.apple.LocalAuthentication Code=-6 "User has denied the use of biometry for this app." UserInfo={NSLocalizedDescription=User has denied the use of biometry for this app.}
(lldb) po context.biometryType
LABiometryTypeNone
NOTE: I Don't want to use passcode authentication. I just need to know device is support Touch ID or Face ID
最佳答案
使用 LAContext
的属性 biometryType
检查和评估可用的生物识别策略。 (对于密码身份验证,当生物识别失败时,使用:LAPolicyDeviceOwnerAuthentication
)
试试看:
LAContext *laContext = [[LAContext alloc] init];
NSError *error;
// For a passcode authentication , when biometric fails, use: LAPolicyDeviceOwnerAuthentication
//if ([laContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthentication error:&error]) {
if ([laContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
if (error != NULL) {
// handle error
} else {
if (@available(iOS 11, *)) {
if (laContext.biometryType == LABiometryTypeFaceID) {
//localizedReason = "Unlock using Face ID"
NSLog(@"FaceId support");
} else if (laContext.biometryType == LABiometryTypeTouchID) {
//localizedReason = "Unlock using Touch ID"
NSLog(@"TouchId support");
} else {
//localizedReason = "Unlock using Application Passcode"
NSLog(@"No biometric support or Denied biometric support");
}
} else {
// Fallback on earlier versions
}
[laContext evaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Test Reason" reply:^(BOOL success, NSError * _Nullable error) {
if (error != NULL) {
// handle error
} else if (success) {
// handle success response
} else {
// handle false response
}
}];
}
}
关于ios - 用户拒绝使用时无法检查 Face ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48382721/