在初始化之前由闭包捕获的变量'isVericated' - Swift



我正在尝试从名为identityVerification的函数返回一个名为isVerificated的布尔变量,以便我可以在tableView函数中使用它。该函数identityVerification通过面容 ID 或触摸 ID 和返回的变量isVerificated启动身份验证,从而告知验证是否成功。

换句话说:我的目标是当你点击TableView中的一个单元格时,它应该首先使用Face ID或Touch ID开始身份验证。然后,身份验证成功后,将打开一个新的视图控制器。如果身份验证失败,应用程序将显示一个警报控制器,其中包含一条消息:"身份验证失败",并且不会打开新的视图控制器。

问题:当我运行应用程序时,出现两个错误:

  1. (!变量"已验证"在初始化之前由闭包捕获
  2. (!初始化前使用的变量"已验证">

这是代码:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let IDisVarificated = identityVerification()    //here the "identityVerification" function is started
if IDisVarificated == true {
if let vc = storyboard?.instantiateViewController(withIdentifier: "detail") as? PasswordTVcontroller {
navigationController?.pushViewController(vc, animated: true)
}
} else {return}
}


func identityVerification() -> Bool {
var isVerificated: Bool
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {   //HERE IS ERROR NUMBER 1
let reason = "Identify yourself!"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
[weak self] success, authenticationError in
DispatchQueue.main.async {
if success {
isVerificated = true    //verification was successfull
} else {
let ac = UIAlertController(title: "Authentication failed", message: "You could not be verified; please try again.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self?.present(ac, animated: true)
isVerificated = false     //verification failed
}
}
}
} else {
let ac = UIAlertController(title: "Biometry unavailable", message: "Your device is not configured for biometric authentication.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self.present(ac, animated: true)
isVerificated = false      //verification failed
}

return isVerificated      //returning a variable with boolean value that tells if verification was successfull or not, HERE IS ERROR NUMBER 2
}

感谢您的帮助!

这种问题总是出现在不熟悉异步函数的人身上。

不能编写从LAContext函数返回结果的函数evaluatePolicy(_:localizedReason:reply:)。时期。

该函数是异步的。这意味着它不会返回结果。它会立即返回,然后在后台执行您要求它执行的工作。工作完成后,它将调用reply闭包。

您的代码尝试在闭包中设置局部变量isVerificated。那行不通。在运行闭包时,您的函数已经返回。

您需要重写函数 identityVerification 以采用闭包而不是返回函数。重构的函数可能如下所示:

func identityVerification(completion: (Bool) -> Void) {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {   //HERE IS ERROR NUMBER 1
let reason = "Identify yourself!"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) {
[weak self] success, authenticationError in
DispatchQueue.main.async {
if success {
completion(true)
} else {
let ac = UIAlertController(title: "Authentication failed", message: "You could not be verified; please try again.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self?.present(ac, animated: true)
completion(false)
}
}
}
} else {
let ac = UIAlertController(title: "Biometry unavailable", message: "Your device is not configured for biometric authentication.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
self.present(ac, animated: true)
completion(false)
}
}

相关内容

  • 没有找到相关文章

最新更新