iosswiftuinavigationcontrollerapple-push-notificationsrootview

Swift didReceiveRemoteNotification - Navigate to rootviewcontroller no matter where in app user is located (Now With Error Message)


Upon receiving push notification in the IOS Swift app, I would like to do two things based on content in notification:

  1. Either navigate to screen (deeplink) so i would have to navigate from rootviewcontroller through several screens.

  2. Navigate to rootviewcontroller, no matter where the user is located in the app.

The second one I see as prerequisite for the first one.

I know that I need to place code in these two functions:

Error message: 'UIViewcontroller?' does not have a member named 'navigationController'

In the file AppDelegate.swift:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) 
{
    println("didReceiveRemoteNotification")
    //Navigate to rootviewcontroller
    var rootViewController = self.window!.rootViewController
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CurrentShows")
        as ViewController

    //rootViewController.navigationController?
    //    .popToViewController(setViewController, animated: false)
}

Solution

  • Try this code In your method :

    var rootViewController = self.window!.rootViewController
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
    var setViewController = mainStoryboard
        .instantiateViewControllerWithIdentifier("CurrentShows")
            as ViewController_CurrentShows
    
    rootViewController
        .navigationController
        .popToViewController(setViewController, animated: false)