iOS,仅对应用程序功能的子集使用生物认证
我正在开发一个应用程序,在这个应用程序中,用户只需要对其中一些功能进行身份验证,但在其他功能中,他们应该保持“匿名”。当用户尝试访问特定功能时,是否有办法在应用程序中实施生物特征认证iOS,仅对应用程序功能的子集使用生物认证,ios,swift,Ios,Swift,我正在开发一个应用程序,在这个应用程序中,用户只需要对其中一些功能进行身份验证,但在其他功能中,他们应该保持“匿名”。当用户尝试访问特定功能时,是否有办法在应用程序中实施生物特征认证 例如,让我们假设,要参加应用程序内的开放式讨论论坛,他们不需要进行身份验证,但一旦他们尝试发送直接消息,他们将需要使用触摸ID或面部ID进行身份验证。这可以做到吗,还是只能在用户打开应用程序时使用生物识别身份验证?是,你可以在任何地方使用它。只需请求验证,然后在验证成功完成后,显示另一个ViewController
例如,让我们假设,要参加应用程序内的开放式讨论论坛,他们不需要进行身份验证,但一旦他们尝试发送直接消息,他们将需要使用触摸ID或面部ID进行身份验证。这可以做到吗,还是只能在用户打开应用程序时使用生物识别身份验证?是,你可以在任何地方使用它。只需请求验证,然后在验证成功完成后,显示另一个ViewController。是的,您可以在任何地方使用它。只需请求身份验证,然后在成功完成身份验证后,显示另一个ViewController。您可以在任何地方进行生物识别身份验证,以下是一个示例,您可以尝试:
func authenticationWithTouchID(completion: (Bool) -> ()) {
let localAuthenticationContext = LAContext()
localAuthenticationContext.localizedFallbackTitle = "Use Passcode"
var authError: NSError?
let reasonString = "To access the secure data"
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
if success {
completion(true)
} else {
//TODO: User did not authenticate successfully, look at the error and take appropriate action
guard let error = evaluateError else { return }
print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
//TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
completion(false)
}
}
} else {
guard let error = authError else { return }
//TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
}
}
func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
var message = ""
if #available(iOS 11.0, macOS 10.13, *) {
switch errorCode {
case LAError.biometryNotAvailable.rawValue:
message = "Authentication could not start because the device does not support biometric authentication."
case LAError.biometryLockout.rawValue:
message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."
case LAError.biometryNotEnrolled.rawValue:
message = "Authentication could not start because the user has not enrolled in biometric authentication."
default:
message = "Did not find error code on LAError object"
}
} else {
switch errorCode {
case LAError.touchIDLockout.rawValue:
message = "Too many failed attempts."
case LAError.touchIDNotAvailable.rawValue:
message = "TouchID is not available on the device"
case LAError.touchIDNotEnrolled.rawValue:
message = "TouchID is not enrolled on the device"
default:
message = "Did not find error code on LAError object"
}
}
return message;
}
func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
var message = ""
switch errorCode {
case LAError.authenticationFailed.rawValue:
message = "The user failed to provide valid credentials"
case LAError.appCancel.rawValue:
message = "Authentication was cancelled by application"
case LAError.invalidContext.rawValue:
message = "The context is invalid"
case LAError.notInteractive.rawValue:
message = "Not interactive"
case LAError.passcodeNotSet.rawValue:
message = "Passcode is not set on the device"
case LAError.systemCancel.rawValue:
message = "Authentication was cancelled by the system"
case LAError.userCancel.rawValue:
message = "The user did cancel"
case LAError.userFallback.rawValue:
message = "The user chose to use the fallback"
default:
message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
}
return message
}
现在,您可以在任何地方使用此功能
authenticationWithTouchID(completion: {(success) in
if success {
//TODO: User authenticated successfully, take appropriate action
} else {
//TODO: User authenticateion failed, take appropriate action
}
})
您可以在任何地方进行生物特征验证,以下是您可以尝试的示例:
func authenticationWithTouchID(completion: (Bool) -> ()) {
let localAuthenticationContext = LAContext()
localAuthenticationContext.localizedFallbackTitle = "Use Passcode"
var authError: NSError?
let reasonString = "To access the secure data"
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
if success {
completion(true)
} else {
//TODO: User did not authenticate successfully, look at the error and take appropriate action
guard let error = evaluateError else { return }
print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
//TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
completion(false)
}
}
} else {
guard let error = authError else { return }
//TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
}
}
func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
var message = ""
if #available(iOS 11.0, macOS 10.13, *) {
switch errorCode {
case LAError.biometryNotAvailable.rawValue:
message = "Authentication could not start because the device does not support biometric authentication."
case LAError.biometryLockout.rawValue:
message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."
case LAError.biometryNotEnrolled.rawValue:
message = "Authentication could not start because the user has not enrolled in biometric authentication."
default:
message = "Did not find error code on LAError object"
}
} else {
switch errorCode {
case LAError.touchIDLockout.rawValue:
message = "Too many failed attempts."
case LAError.touchIDNotAvailable.rawValue:
message = "TouchID is not available on the device"
case LAError.touchIDNotEnrolled.rawValue:
message = "TouchID is not enrolled on the device"
default:
message = "Did not find error code on LAError object"
}
}
return message;
}
func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
var message = ""
switch errorCode {
case LAError.authenticationFailed.rawValue:
message = "The user failed to provide valid credentials"
case LAError.appCancel.rawValue:
message = "Authentication was cancelled by application"
case LAError.invalidContext.rawValue:
message = "The context is invalid"
case LAError.notInteractive.rawValue:
message = "Not interactive"
case LAError.passcodeNotSet.rawValue:
message = "Passcode is not set on the device"
case LAError.systemCancel.rawValue:
message = "Authentication was cancelled by the system"
case LAError.userCancel.rawValue:
message = "The user did cancel"
case LAError.userFallback.rawValue:
message = "The user chose to use the fallback"
default:
message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
}
return message
}
现在,您可以在任何地方使用此功能
authenticationWithTouchID(completion: {(success) in
if success {
//TODO: User authenticated successfully, take appropriate action
} else {
//TODO: User authenticateion failed, take appropriate action
}
})