0
votes

My app rootviewcontroller is with tab bar, but when I use firebase push notification it only allows me "present" method. I want to view the controller with push (like child) to have the tab bar and back option when i view this view controller.

In my code I want to transfer from present to PUSH but i cant find solution. currentController.present(controller, animated: true, completion: nil)

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

    if Defaults.hasKey(.logged), let logged = Defaults[.logged], logged == true {

        //TODO


            if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ProductDetailsViewController") as? ProductDetailsViewController {
                controller.productFromNotificationByCode(code: userInfo["product_code"] as! String, productID: userInfo["product_id"] as! String)
                if let window = self.window, let rootViewController = window.rootViewController {
                    var currentController = rootViewController
                    while let presentedController = currentController.presentedViewController {
                        currentController = presentedController
                    }
                    currentController.present(controller, animated: true, completion: nil)
                }
            }
        }
        print(userInfo)
    }
    completionHandler()
}
2
You'll need to load tab bar controller from the storyboard first and so on - Mannopson

2 Answers

1
votes

The easiest way to do it would be to wrap your root view controller in a UINavigationController in your storyboard. This would make the actual root controller a UINavigationController rather than a UITabBarController.

once that is done you can simply do

if let window = self.window, let rootNavController = window.rootViewController as? UINavigationController {
   navigationController?.pushViewController(controller, animated: true)
}

Alternately, if all you're really looking for is a "Slide from the right" animation and don't need a full navigation stack, you could write a custom view controller transition that gave a similar effect using modal controllers.

0
votes

I have not tested yet, but I believe it's just works. Try this:

func pushViewController() {

    let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController

    let navigationController = storyboard.instantiateViewController(withIdentifier: "YourNavigationController") as! UINavigationController

    let productDetailsViewController = storyboard.instantiateViewController(withIdentifier: "ProductDetailsViewController") as! ProductDetailsViewController

    tabBarController.viewControllers = [navigationController]
    if tabBarController.selectedViewController == navigationController {
        navigationController.pushViewController(productDetailsViewController, animated: true)
    }



    self.window = UIWindow.init(frame: UIScreen.main.bounds)
    self.window?.rootViewController = tabBarController
    self.window?.makeKeyAndVisible()

}