【发布时间】:2022-01-15 19:45:52
【问题描述】:
我试图从名为identityVerification 的函数中返回一个名为isVerificated 的布尔变量,因此我可以在tableView 函数中使用它。函数identityVerification通过Face ID或Touch ID启动身份验证,返回变量isVerificated,告知验证是否成功。
换句话说: 我的目标是当你点击 TableView 中的一个单元格时,它应该首先使用 Face ID 或 Touch ID 开始身份验证。然后身份验证成功后,会打开一个新的ViewController。如果身份验证失败,应用会显示一个 AlertController 并显示消息:“Authentication failed”,并且不会打开新的 ViewController。
问题: 当我运行应用程序时出现两个错误:
- (!) 变量“isVericated”在被初始化之前被闭包捕获
- (!) 变量“isVericated”在初始化之前使用
代码如下:
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
}
感谢您的帮助!
【问题讨论】:
-
我认为你可以通过在声明
var isVerificated = false上初始化变量来解决问题 -
将
isVerificated声明为初始值为var的false将使编译器警告静音,但它不能解决您不会得到异步结果的事实当您从identityVerification()函数返回时调用。请参阅我的答案以获得正确的解决方案。 -
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
标签: ios swift uikit closures face-id