我正在创建一个新的应用程序,并且要放在一个隐藏的文件夹中。可通过人脸ID /触摸ID进行访问。我已经实现了代码,但是当我运行应用程序并使用Face ID时。应用程序崩溃并显示错误“ NSInternalInconsistencyException”
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.
在我的Viewcontroller中,将视图设置为:
override func viewDidLoad() {
super.viewDidLoad()
let cornerRadius : CGFloat = 10.0
containerView.layer.cornerRadius = cornerRadius
tableView.clipsToBounds = true
tableView.layer.cornerRadius = 10.0
// 1
let context = LAContext()
var error: NSError?
// 2
// check if Touch ID is available
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
// 3
let reason = "Authenticate with Biometrics"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: {(success, error) in
// 4
if success {
self.showAlertController("Biometrics Authentication Succeeded")
} else {
self.showAlertController("Biometrics Authentication Failed")
}
})
}
// 5
else {
showAlertController("Biometrics not available")
}
}
我希望Face ID / Touch ID能够按预期工作,并且如果经过验证不会崩溃。
您正在后台线程上进行UI调用(显示警报),因此出现此问题。
改变以下
if success {
self.showAlertController("Biometrics Authentication Succeeded")
} else {
self.showAlertController("Biometrics Authentication Failed")
}
至
DispatchQueue.main.async {
if success {
self.showAlertController("Biometrics Authentication Succeeded")
} else {
self.showAlertController("Biometrics Authentication Failed")
}
}
如果要更新UI部件,请记住始终使用DispatchQueue.main.async运行这些任务。UI更改必须在主线程中运行。
如果您向下滚动到Section,还可以查看使用Face ID或Touch ID将用户登录到您的应用程序-Apple文档Evaluate a Policy
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句