'Swift: NavigationController show nil when open from tapped push notif

At first i try to present viewController and show nothing. Then, i print the navigation controller and return nil. So i expect that's the reason why viewController is not presenting. This error happen when open it from push notification. If i open it normally, it works well

App delegate

guard let tabBarController = window?.rootViewController as? UITabBarController else {
                              return
                            }

                            guard let tabBarControllers = tabBarController.viewControllers,
                              let listIndex = tabBarControllers.firstIndex(where: { $0 is UINavigationController }),
                              let listViewController = tabBarControllers[listIndex] as? UINavigationController else { return }

                      
                          
                            tabBarController.selectedIndex = listIndex
                    
                            var navigationController = UIViewController()
                            let viewModel = BannerViewViewModel(with: 60 ?? 0)
                            let viewController = BannerViewController(viewModel: viewModel) { [weak self] prdCode in
                                self?.window!.rootViewController?.dismiss(animated: true, completion:{
                          
                                
                               
                                promoVC.handleStepCode(with: .notRedirecting, productCode: prdCode, info: [:])
                           
                            })
                                
                       
                    }
                   
                    navigationController = UINavigationController(rootViewController: viewController)
                    navigationController.modalPresentationStyle = .fullScreen
                    listViewController.present(navigationController, animated: true, completion: nil)
              
                    window?.rootViewController = tabBarController
                    window?.makeKeyAndVisible()

Code above run to open banner and then do completion -> promoVC.handlestepcode

promoVC.handlestepcode is do function from another view controller

Home Controller

var  promoVC =  HomeController(viewModel: HomeViewModel())

 public func handleStepCode(with stepCode: StepCode, productCode: ProductCode, info: [String:Any]) {
       
        switch (stepCode, productCode) {
       
        case (.notRedirecting, .prco):
            handleStep(with: .notRedirectingprco)
        }
    }

func handleStep(with step: ABCStepCode) {
        print("masuk handlestep")
        switch step {
      
        case .notRedirectingprco:
            print("masuk kiniflexi")
            self.handleprco(with: .main)
        }
    }

    func handleprco(with type: prcostep) {
            print("try to open prcostep")
            let viewController = ABCNavigationController(with: type) { [weak self] (result) in
                DispatchQueue.main.async { [weak self] in
                    switch result {
                    case .success: break
                    case .failure(let error):
                        self?.show(with: error)
                    }
                }
            }
            guard let window = UIApplication.shared.windows.first else {
                return
            }
            // Check we can access the root viewController
            guard let vc = window.rootViewController else {
                return
            }
            // Check the root vc is the type that we want to dismiss
            print("testwindvc")
            print(window)
            print(vc)
            print(navigationController) **//SHOW NIL**
           
                navigationController?.present(viewController, animated: true, completion: nil)
            
            
        }

in function handleprco -> try to print navigationcontroller, it show nil if i open it from push notif, but if i open it normally, navigation controller works well ?

How to make navigationcontroller is not nil?

try to create navigationcontroller if it's nil inside handleprco

if (navigationController == nil){
            let navigationController = UINavigationController()
            navigationController.pushViewController(viewController, animated: true)
        }else{
            navigationController?.present(viewController, animated: true, completion: nil)
        }

but error push navigationcontroller is not supported



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source