我有一个应用程序,我需要非常安全。因此,应用程序前景化的用户需要通过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()
,我会:
致命错误:尝试读取无主参考但该对象已被解除分配
答案 0 :(得分:1)
您的context
对象正在被取消分配。
将代码更改为此,因此上下文对象具有引用
var context: LAContext!
func authenticateReturningUser() {
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")
}
}
}
}
}
}