Search code examples
iospush-notificationuisplitviewcontrollerpresentviewcontroller

didReceiveRemoteNotification presentViewController within splitView


I have been trying to open a specific view on my App when a push notifications arrives, but this view is being presented modally by a detailed view of a split view and I haven't been able to do it.

This is my story board

I have tried one of the many answers that are on the topic which is just presenting EventsViewController, but clearly this way leaves the VC isolated and no longer connected to the split view.

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let navigationController = storyboard.instantiateViewControllerWithIdentifier("NavigationControllerMessages") as! UINavigationController
    let dVC:MessagesViewController = navigationController.topViewController as! MessagesViewController
    dVC.vehicleLicensePlate = "ABC"
    dVC.vehicleName = "My car"
    self.window?.rootViewController?.presentViewController(navigationController, animated: true, completion: {})

}

Also I have tried presenting the split view which actually works but I don't know how to climb the hierarchy view to get to EventsViewController:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("SplitInStoryboard") as! SplitViewController
    self.window?.rootViewController?.presentViewController(destinationViewController, animated: true, completion:nil)

}

The question is how to present EventsViewController so I can connect all the views between the root view controller and my view?

Hope anyone can help me, please. I have been struggling with this for many hours. Thanks in advance!


Solution

  • I finally found the answer! It is based on @StepanGeneralov´s answer https://stackoverflow.com/a/21297223/6144027. I post it here because this might help someone one day.

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mvc = storyboard.instantiateViewControllerWithIdentifier("MessagesInStoryboard") as! MessagesViewController
        mvc.vehicleName = "My car"
        mvc.vehicleLicensePlate = "ABC"
        let navController = UINavigationController(rootViewController: mvc) // Creating a navigation controller for mvc
    
        var topRootViewController : UIViewController! = self.window?.rootViewController
        while let vvc = topRootViewController.presentedViewController{
            topRootViewController = vvc
    
        }
        topRootViewController.presentViewController(navController, animated: true, completion: nil)
    

    By the way, as @guillaume pointed out, we should implement application:didReceiveRemoteNotification:fetchCompletionHandler: instead of just didReceiveRemoteNotification.