1

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:

  • didReceiveRemoteNotification
  • didReceiveLocalNotification

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)
}
Claus Machholdt
  • 187
  • 3
  • 12

3 Answers3

0

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)
holroy
  • 3,047
  • 25
  • 41
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Im running code from appDelegate.swift Code doesn't seem to work. "UIViewController? does not have a member named navigationcontroller. Where do I set the viewControllerIidentifier? I imagine that it is the viewcontroller which i wanna navigate to which should be placed here right? – Claus Machholdt Feb 23 '15 at 14:13
  • Yes..select the viewcontroller and in identity inspecter window set storyboard id there. – Dharmesh Kheni Feb 23 '15 at 16:20
  • It makes the app crash. – Claus Machholdt Mar 01 '15 at 20:18
  • Hmm. Dificult to show you a log as it is in production. Can't test push notificaations in test environment. Or I don't know how. – Claus Machholdt Mar 02 '15 at 11:04
  • Just asking: Have you pushed the viewController you are popping here? ;-) Could it be you have a similar problem as [this one](http://stackoverflow.com/a/27415961/1548472)? – holroy Mar 09 '15 at 22:10
  • If i put this code in a viewcontroller it still doesnt work, though i have another error message: "Viewcontroller" does not have amember named 'window' – Claus Machholdt Mar 11 '15 at 14:29
0

Needed to create a variable: var window: UIWindow

Code below was implemented in appdelegate, in: func didBecomeActive()....

let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let rootController = storyboard.instantiateViewControllerWithIdentifier("Login") as Login_ViewController

            self.window?.rootViewController?.dismissViewControllerAnimated(false, completion:
            {
                if self.window != nil
                {
                    self.window!.rootViewController = rootController
                }
            })
Claus Machholdt
  • 187
  • 3
  • 12
0

I wrote a simple class to navigate to any view controller in the view hierarchy from anywhere in one line of code by just passing the class type, so the code you'll write will be also decoupled from the view hierarchy itself, for instance:

Navigator.find(MyViewController.self)?.doSomethingSync()
Navigator.navigate(to: MyViewController.self)?.doSomethingSync()

..or you can execute methods asynchronously on the main thread also:

Navigator.navigate(to: MyViewController.self) { (MyViewControllerContainer, MyViewControllerInstance) in
    MyViewControllerInstance?.doSomethingAsync()
}

Here the GitHub project link: https://github.com/oblq/Navigator

Marco M
  • 1,215
  • 12
  • 12