我正在创建一个新的应用程序,我想把一个隐藏的文件夹。通过Face /Touch ID访问。我已经实现了代码,但是当我运行应用程序时,使用了Face。
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.在我的视图控制器中,我将视图设置为:
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")
}
}我想要脸ID /触摸ID按预期工作,如果得到验证,不要崩溃。
发布于 2019-08-30 13:56:19
您正在后台线程上进行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更改必须在主线程中运行。
如果向下滚动到使用Face或Touch ID将用户登录到应用程序中- Apple文档节,也可以查看Evaluate a Policy。
发布于 2019-08-30 13:52:54
错误非常明显:
在从主线程访问布局引擎之后,不能从后台线程执行对布局引擎的修改。
除了主线程之外,您不能从任何线程编辑UI,因为您试图对evaluatePolicy回调进行编辑。应该将UI修改代码放在对DispatchQueue.main.sync的调用中
https://stackoverflow.com/questions/57727944
复制相似问题