当应用程序进入后台和前台时显示带有通知中心的登录屏幕,Swift
Present login screen with notification center when app enters background and foreground, Swift
我有登录屏幕,我想展示它或只是将它添加到导航中,现在没关系,仍然不确定每次我的应用程序返回前台时我希望它是哪种方式.下面的代码部分有效,它确实启动了人脸 ID,但实际上没有显示整个登录屏幕,我仍然可以看到进入后台之前打开的最后一个屏幕。我想用登录屏幕隐藏所有内容。当我在 appMovedToBackground 函数中打印 childrens 时,会打印正确的视图控制器,这意味着它已添加,但正如我所说,它没有显示,只能看到面部 ID 中的图标。下面的代码仅在一个视图控制器上,目前,我希望将其应用于我所有的视图控制器,我应该将功能移动到 appdelegate 吗?提前致谢。
let notificationCenter = NotificationCenter.default
override func viewDidLoad() {
super.viewDidLoad()
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(appCameToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func appMovedToBackground() {
print("its in background")
let authVC = AuthViewController()
navigationController?.topViewController?.addChild(authVC)
}
@objc func appCameToForeground() {
print("app enters foreground")
if let authVC = navigationController?.topViewController?.children.first as? AuthViewController {
print("willEnterForegroundNotification")
authVC.checkBiometricsAndStart()
}
}
这里需要正确添加vc
@objc func appMovedToBackground() {
print("its in background")
guard let added = navigationController?.topViewController else { return }
let authVC = AuthViewController()
authVC.view.frame = UIScreen.main.bounds
added.view.addSubview(authVC.view)
added.addChild(authVC)
}
关于所有 vcs 都需要它,是的,在 AppDelegate 中实现它是最简单的方法
我有登录屏幕,我想展示它或只是将它添加到导航中,现在没关系,仍然不确定每次我的应用程序返回前台时我希望它是哪种方式.下面的代码部分有效,它确实启动了人脸 ID,但实际上没有显示整个登录屏幕,我仍然可以看到进入后台之前打开的最后一个屏幕。我想用登录屏幕隐藏所有内容。当我在 appMovedToBackground 函数中打印 childrens 时,会打印正确的视图控制器,这意味着它已添加,但正如我所说,它没有显示,只能看到面部 ID 中的图标。下面的代码仅在一个视图控制器上,目前,我希望将其应用于我所有的视图控制器,我应该将功能移动到 appdelegate 吗?提前致谢。
let notificationCenter = NotificationCenter.default
override func viewDidLoad() {
super.viewDidLoad()
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(appCameToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func appMovedToBackground() {
print("its in background")
let authVC = AuthViewController()
navigationController?.topViewController?.addChild(authVC)
}
@objc func appCameToForeground() {
print("app enters foreground")
if let authVC = navigationController?.topViewController?.children.first as? AuthViewController {
print("willEnterForegroundNotification")
authVC.checkBiometricsAndStart()
}
}
这里需要正确添加vc
@objc func appMovedToBackground() {
print("its in background")
guard let added = navigationController?.topViewController else { return }
let authVC = AuthViewController()
authVC.view.frame = UIScreen.main.bounds
added.view.addSubview(authVC.view)
added.addChild(authVC)
}
关于所有 vcs 都需要它,是的,在 AppDelegate 中实现它是最简单的方法