我有一个应用程序,我需要非常安全。因此,前台应用程序的用户需要通过 TouchID、FaceID 或他们的电话密码验证自己才能继续。
我能够显示和删除coverVC,但是在我BackgroundViewController的(充当 的那个coverVC)中,我想在应用程序处于前台时提示用户进行生物身份验证
我正在尝试将一个handler函数传递BackgroundViewController给在正确的时间触发,但它不起作用,我不清楚我做错了什么。会喜欢这里的任何帮助。谢谢!
在我的AppDelegate:
func applicationWillResignActive(_ application: UIApplication) {
coverVC = BackgroundViewController()
coverWindow = UIWindow(frame: UIScreen.main.bounds)
let existingTopWindow = UIApplication.shared.windows.last
coverWindow?.windowLevel = existingTopWindow!.windowLevel + 1
coverVC!.view.frame = coverWindow!.bounds
coverWindow?.rootViewController = coverVC
coverWindow?.makeKeyAndVisible()
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if coverWindow != nil {
self.coverVC?.handler = {
DispatchQueue.main.async {
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
self.coverVC?.view.alpha = 0
}) { _ in
self.coverWindow!.isHidden = true
self.coverWindow!.rootViewController = nil
self.coverWindow = nil
self.coverVC = nil
}
}
}
self.coverVC?.authenticateReturningUser()
}
}
在我的BackgroundViewController.swift:
class BackgroundViewController: UIViewController {
var handler: (() -> Void) = {}
(viewDidLoad and other stuff in here)
func authenticateReturningUser() {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = "Verify that this is your device to continue."
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in
DispatchQueue.main.async {
if success {
if let _self = self {
_self.handler()
} else {
print("this is hitting")
}
}
}
}
}
private func showError(title: String, message: String, buttonText text: String = "Okay") {
let ac = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: text, style: .default) { _ in
self.handler()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LogOut"), object: nil)
})
present(ac, animated: true)
}
编辑:
如果我设置[weak self]并[unowned self]调用self.handler(),我会得到:
致命错误:尝试读取无主引用但对象已被释放