我正在尝试获取TouchID来验证我的应用上的操作。如果成功,我需要它来显示ImageView。
这是我在ViewController中拥有的代码
@IBOutlet weak var notificationImage: UIImageView!
@IBAction func touchBtn(_ sender: UIButton) {
context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Authenticate"
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in
if success {
print("In closure")
// Move to the main thread because a state update triggers UI changes.
DispatchQueue.main.async{
print("In main thread: Success")
self.notificationImage.isHidden = false
}
} else {
DispatchQueue.main.async(execute: {
print("In main thread: Failed")
})
}
}
}
}
我能够使用TouchID成功进行身份验证。打印“ Inclosure”。但是DispatchQueue.main.async中的块不执行,并且我无法更新notificationImage:UIImageView。它也不打印sync {}块中的任何内容。
我试图禁用主线程检查器并删除DispatchQueue.main.async部分,但是视图有时会刷新,而有时却不会。我想使用推荐的做法并从主线程更新UI。
我正在使用Xcode 9 Swift 4 iOS 11.4
这里可能出什么问题了。
答案 0 :(得分:0)
尝试此代码
@IBAction func touchBtn(_ sender: UIButton) {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Authenticate"
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in
if success {
do {
print("In closure")
print("In main thread: Success")
self.notificationImage.isHidden = false
} catch let error as NSError {
print(error.localizedDescription)
print(error.description)
}
} else {
DispatchQueue.main.async(execute: {
print("In main thread: Failed")
})
}
}
}
}