I am implementing the iOS app with coordinator pattern and I need to show some privacy screen when the app enters background.
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
}
}
On entry, the sceneWillEnterForeground()
is called. On going to background, sceneDidEnterBackground()
is not called. What am I missing?
Found it myself in 10 minutes after posting the question, I used initially wrong methods. This will work for what I'm trying to achieve:
func sceneDidBecomeActive(_ scene: UIScene) {
hidePrivacyProtectionWindow()
}
func sceneWillResignActive(_ scene: UIScene) {
showPrivacyProtectionWindow()
}