iosswiftuiprogram-entry-pointios14uihostingcontroller

iOS 14: Scene lifecycle events not getting hit after setting the application root view controller in SwiftUI @main


My test code:

import SwiftUI

@main
struct TestingIOS14App: App {
    @Environment(\.scenePhase) var scenePhase
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .withHostingWindow { window in
                    let hostingController = UIHostingController(rootView: ContentView())
                    let mainNavigationController = UINavigationController(rootViewController: hostingController)
                    mainNavigationController.navigationBar.isHidden = true
                    window?.rootViewController = mainNavigationController
                }
        }
        .onChange(of: scenePhase) { newScenePhase in
            switch newScenePhase {
            case .active:
                print("App is active")
            case .inactive:
                print("App is inactive")
            case .background:
                print("App is in background")
            @unknown default:
                print("Oh - interesting: I received an unexpected new value.")
            }
        }
    }
}

extension View {
    func withHostingWindow(_ callback: @escaping (UIWindow?) -> Void) -> some View {
        self.background(HostingWindowFinder(callback: callback))
    }
}

struct HostingWindowFinder: UIViewRepresentable {
    var callback: (UIWindow?) -> Void

    func makeUIView(context: Context) -> UIView {
        let view = UIView()
        DispatchQueue.main.async { [weak view] in
            self.callback(view?.window)
        }
        return view
    }

    func updateUIView(_ uiView: UIView, context: Context) { }
}

@Asperi's answer here and the code above seem to works but the scene lifecycles in .onChange(of: don't seem to hit except the active phase when the app is launched initially. Not sure what I am doing wrong, would appreciate any help on this please.

Many thanks :)


Solution

  • The difference between your code and the answer you link to is that you're completely replacing the app's view hierarchy. Notice how they use

    if let controller = window?.rootViewController
    

    and instead you're assigning a new root view controller

    window?.rootViewController = mainNavigationController
    

    My guess is that's what's causing your problem.

    If all you want is to hide the navigation bar, though, there are SwiftUI methods to do that.