问题描述
我正在使用协调器模式实现 iOS 应用程序,当应用程序进入后台时,我需要显示一些隐私屏幕。
class SceneDelegate: UIResponder,UIWindowSceneDelegate {
var window: UIWindow?
var coordinator: MainCoordinator?
func scene(_ scene: UIScene,willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = scene as? UIWindowScene else { return }
if let registry = DependencyResolver.shared as? DependencyRegistry {
DependencyGraph.setup(for: registry)
}
let navController = UINavigationController()
navController.setNavigationBarHidden(true,animated: false)
coordinator = MainCoordinator(navigationController: navController)
coordinator?.start()
self.window = UIWindow.init(windowScene: scene)
window?.rootViewController = navController
window?.makeKeyAndVisible()
}
func sceneWillEnterForeground(_ scene: UIScene) {
hidePrivacyProtectionWindow()
}
func sceneDidEnterBackground(_ scene: UIScene) {
showPrivacyProtectionWindow()
}
...
// MARK: Privacy Protection
private var privacyProtectionWindow: UIWindow?
private func showPrivacyProtectionWindow() {
guard let windowScene = self.window?.windowScene else {
return
}
privacyProtectionWindow = UIWindow(windowScene: windowScene)
let storyboard = UIStoryboard(name: "LaunchScreen",bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "splash")
privacyProtectionWindow?.rootViewController = vc
privacyProtectionWindow?.windowLevel = .alert + 1
privacyProtectionWindow?.makeKeyAndVisible()
}
private func hidePrivacyProtectionWindow() {
privacyProtectionWindow?.isHidden = true
privacyProtectionWindow = nil
}
}
在进入时,调用 sceneWillEnterForeground()
。在进入后台时,不会调用 sceneDidEnterBackground()
。我错过了什么?
解决方法
在发布问题后 10 分钟内我自己找到了,我最初使用了错误的方法。这将适用于我正在努力实现的目标:
func sceneDidBecomeActive(_ scene: UIScene) {
hidePrivacyProtectionWindow()
}
func sceneWillResignActive(_ scene: UIScene) {
showPrivacyProtectionWindow()
}