【发布时间】:2020-02-14 04:47:03
【问题描述】:
我想知道当用户从应用程序的设置中禁用它们时,我如何调用 iOS 以显示从苹果弹出的警报以授予应用程序访问 Face ID/Touch ID 的权限。我知道这是放在 info plist 中的,但是当我从设置中禁用它们时,它不再显示询问:
【问题讨论】:
-
您只能询问一次。之后,您需要将用户引导回设置以启用它。
标签: ios swift touch-id face-id
我想知道当用户从应用程序的设置中禁用它们时,我如何调用 iOS 以显示从苹果弹出的警报以授予应用程序访问 Face ID/Touch ID 的权限。我知道这是放在 info plist 中的,但是当我从设置中禁用它们时,它不再显示询问:
【问题讨论】:
标签: ios swift touch-id face-id
您需要检查设备是否可以通过生物识别进行身份验证。
让我们在调用函数进行身份验证之前执行此操作。
func canAuthenByBioMetrics() -> Bool {
let context = LAContext()
var authError: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
return true
} else {
return false
}
}
显示你的代码会是这样的:
if self.canAuthenByBioMetrics() {
// Do you authentication
} else {
// Ask user for enable permission or setup biometric if needed
}
【讨论】:
正如@Paulw11 提到的,您只能询问一次。如果用户拒绝访问,您可以做的最好的事情是询问他们是否要转到“设置”以允许生物识别。代码是这样的:
let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
print("Settings opened: \(success)") // Prints true
})
}
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
如this answer 所示。
请记住,这会将用户从应用程序中移除,但到目前为止还没有其他方法。
【讨论】:
如果在设置中关闭了 Face ID,则必须将其重定向到设置才能重新打开。
【讨论】: