![](/img/trans.png)
[英]Request iOS face id permission again when it was denied first time by user
[英]Can't check Face ID when user denied to use
如果設備支持Face ID或Touch ID,我想對用戶采取不同的操作。
使用Face ID 時,iOS 會詢問使用權限。 (與觸控 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
注意:我不想使用密碼驗證。 我只需要知道設備是否支持 Touch ID 或 Face ID
否。如果用戶拒絕的隱私權限提示與NSFaceIDUsageDescription
,所有未來實例LAContext
將有biometryType
財產.none
一次canEvalutePolicy:error:
叫他們。
為時已晚,但我希望這可以幫助遇到相同問題的人。
LAContext().canEvaluatePolicy( .deviceOwnerAuthentication , error: nil) LAContext().canEvaluatePolicy( .deviceOwnerAuthenticationWithBiometrics , error: nil)
deviceOwnerAuthentication和deviceOwnerAuthenticationWithBiometrics之間的區別是,第一個會告訴您設備是否具有身份驗證方法。第二個具有相同的行為,但只有用戶接受了權限才有效。
enum BiometryResult: Int {
case faceID
case touchID
case notExist
}
class func biometryType() -> BiometryResult {
let context = LAContext()
if (context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)) {
if (context.biometryType == LABiometryType.faceID) {
return .faceID
} else if (context.biometryType == LABiometryType.touchID) {
return .touchID
} else {
return .notExist
}
}
return .notExist
}
使用屬性biometryType
的LAContext
檢查和評估提供生物識別的策略。 (對於密碼驗證,當生物識別失敗時,使用: 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
}
}];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.