我有一个用Swift编写的应用程序,它使用LocalAuthentication来允许用户使用TouchID或电话密码登录应用程序。 我已经设置了一个通知观察器,以便在应用程序到达Foreground时启动身份验证块,以便用户可以重新进行身份验证。在我按下主页按钮并将应用程序发送到后台后应用程序到达前台时,一切都运行良好,并显示TouchID警报,但是在我从我的应用程序导航到另一个应用程序后出现问题(例如任何其他应用程序通知出现在顶部,然后点击导航到它),然后想要返回我的应用程序:
当我的应用程序返回前台时,TouchID警报不会显示。相反,我的应用程序看起来好像没有经过身份验证(空tableView),在我锁定(deauthenticate)和解锁(身份验证)之后,为了强制进行身份验证,我收到一系列警告,声明“已被其他身份验证取消”且至少有2个TouchID警报。
我联系到TouchID警报被触发,但仍保持隐藏状态,然后通过锁定/解锁操作取消强制另一个警报。
有没有人有类似的问题?
以下是与此问题相关的一些代码:
override func viewDidLoad() {
super.viewDidLoad()
defaults.set(false, forKey: "authenticated")
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillEnterForeground, object: nil, queue: nil, using: {_ in
self.navigationController?.popToRootViewController(animated: true)
self.accounts = []
self.tableView.reloadData()
self.authenticateUser()
self.defaults.set(false, forKey: "authenticated")
})}
func authenticateUser() {
let context = LAContext()
var error: NSError?
let reason = "Identify yourself"
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
print("canEvaluateWithTouchID")
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason:
reason, reply: {success, error in
print("EvaluateWithTouchID")
// Touch ID
DispatchQueue.main.async {
if success {
self.navigationItem.rightBarButtonItem?.isEnabled = true
self.searchBar.isUserInteractionEnabled = true
self.fetchFromCoreData()
self.defaults.set(true, forKey: "authenticated")
print("Success: TouchID")
} else {
self.defaults.set(false, forKey: "authenticated")
self.accounts = []
self.navigationItem.rightBarButtonItem?.isEnabled = false
self.searchBar.isUserInteractionEnabled = false
self.lockButton.title = "Unlock"
self.tableView.reloadData()
switch error!._code {
case Int(kLAErrorAuthenticationFailed):
self.loginAlert(message: error!.localizedDescription)
print("AuthFailed1")
case Int(kLAErrorUserCancel):
self.loginAlert(message: error!.localizedDescription)
print("UserCanceled1")
case Int(kLAErrorBiometryNotEnrolled):
self.loginAlert(message: error!.localizedDescription)
print("biometry1")
case Int(kLAErrorPasscodeNotSet):
self.userFallbackPasswordAlertWith(error: error!)
print("PassNotSet1")
case Int(kLAErrorSystemCancel):
self.loginAlert(message: error!.localizedDescription)
print("SystemCancel1")
case Int(kLAErrorUserFallback):
self.userFallbackPasswordAlertWith(error: error!)
print("UserFallback1")
default:
self.userFallbackPasswordAlertWith(error: error!)
print("default1")
}
}
}
})
} else {
print("canEvaluateWithPasscode")
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason, reply: {success, error in
print("EvaluateWithPasscode")
//No Touch ID
DispatchQueue.main.async {
if success {
self.navigationItem.rightBarButtonItem?.isEnabled = true
self.searchBar.isUserInteractionEnabled = true
self.lockButton.title = "Lock"
self.fetchFromCoreData()
self.defaults.set(true, forKey: "authenticated")
} else {
self.defaults.set(false, forKey: "authenticated")
self.accounts = []
self.navigationItem.rightBarButtonItem?.isEnabled = false
self.searchBar.isUserInteractionEnabled = false
self.lockButton.title = "Unlock"
self.tableView.reloadData()
switch error!._code{
case Int(kLAErrorAuthenticationFailed):
self.loginAlert(message: error!.localizedDescription)
print("AuthFailed2")
case Int(kLAErrorUserCancel):
self.loginAlert(message: error!.localizedDescription)
print("UserCanceled2")
case Int(kLAErrorBiometryNotEnrolled):
self.loginAlert(message: error!.localizedDescription)
print("biometry2")
case Int(kLAErrorPasscodeNotSet):
self.userFallbackPasswordAlertWith(error: error!)
print("PassNotSet2")
case Int(kLAErrorSystemCancel):
self.loginAlert(message: error!.localizedDescription)
print("SystemCancel2")
case Int(kLAErrorUserFallback):
self.userFallbackPasswordAlertWith(error: error!)
print("UserFallback2")
default:
self.userFallbackPasswordAlertWith(error: error!)
print("default2")
}
}
}
})
}
}
@IBAction func lockButton(_ sender: UIBarButtonItem) {
accounts = []
tableView.reloadData()
if lockButton.title == "Unlock" {
authenticateUser()
}
if lockButton.title == "Lock" {
lockButton.title = "Unlock"
}
defaults.set(false, forKey: "authenticated")
}
任何输入都将不胜感激。感谢。