我正在创建一个新应用程序,我想放入一个隐藏文件夹中。可通过 Face ID/Touch 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.
Run Code Online (Sandbox Code Playgroud)
在我的 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")
}
}
Run Code Online (Sandbox Code Playgroud)
我希望 Face ID/Touch ID 能够按预期工作,并且在验证后不会崩溃。
您正在后台线程上进行 UI 调用(显示警报),因此您遇到了此问题。
更改以下内容
if success {
self.showAlertController("Biometrics Authentication Succeeded")
} else {
self.showAlertController("Biometrics Authentication Failed")
}
Run Code Online (Sandbox Code Playgroud)
到
DispatchQueue.main.async {
if success {
self.showAlertController("Biometrics Authentication Succeeded")
} else {
self.showAlertController("Biometrics Authentication Failed")
}
}
Run Code Online (Sandbox Code Playgroud)
如果您要更新 UI 部分,请记住始终使用 DispatchQueue.main.async 来运行这些任务。UI 更改必须在主线程中运行。
如何使用 Swift 4 添加 FaceID/TouchID
您还可以查看使用 Face ID 或 Touch ID 将用户登录到您的应用程序 - Apple 文档如果您向下滚动到Evaluate a Policy部分。
| 归档时间: |
|
| 查看次数: |
4445 次 |
| 最近记录: |