Search code examples
iosswiftswift3uinavigationcontrollerios-universal-links

How to show a specific VC , more like navigate to a stack of VC


So i've implemented universal link in my app and when pressing on a button I open my second app with :

UIApplication.shared.open(aUrl!)

also I'm getting the call in :

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {}

which is an alert.

The question is like the title says..how could I navigate to a specific VC ,(from the FirstApp to the Second which i opened it with universal link), more like mapping a navigation controller stack .

I've writed something like :

        if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyAdsController") as? MyAdsViewController {
        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)
        }
    }

but it's an ugly representation, i need something like the hole navigation stack...

And also , can i make a helper class for this if i want to handle more with universal link? Any suggestion are appreciated.


Solution

  • You need to use the .viewControllers property of your UINavigationController you can do this using setViewControllers(_:animated:) method or modifying directly .viewControllers property where rootViewController will be the viewControllersArray[0] and topViewController will be viewControllersArray[count-1]

    This property description and details can be founded in the UINavigationController documentation

    viewControllers
    

    Property

    The view controllers currently on the navigation stack.

    Declaration SWIFT

    var viewControllers: [UIViewController]
    

    Discussion The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

    Assigning a new array of view controllers to this property is equivalent to calling the setViewControllers:animated: method with the animated parameter set to false.

    example

        let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        let firstViewController = storyboard.instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
        let secondViewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
        let thirdViewController = storyboard.instantiateViewController(withIdentifier: "ThirdViewController") as? ThirdViewController
    
        self.navigationController?.setViewControllers([firstViewController,secondViewController,thirdViewController], animated: false)