Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open notification to specific view

I'm working on an app in Swift using push notifications. So far I have the following code in my AppDelegate:

  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    println("Received alert and opened it")
    debugPrintln(userInfo)

    if application.applicationState == UIApplicationState.Active {
      // App in foreground
      println("App in foreground already")
    } else {
      // App in background
      if let tripId = (userInfo["trip"] as? String)?.toInt() {
        println(tripId)
        Trips.load(tripId) { (responseCode, trip) in
          debugPrintln("Got the trip")
          if let trip = trip {
            if let window = self.window {
              if let rootViewController = window.rootViewController {
                if let storyboard = rootViewController.storyboard {
                  let viewController = storyboard.instantiateViewControllerWithIdentifier("Trip") as! TripViewController
                  viewController.trip = trip
                  rootViewController.presentViewController(viewController, animated: true, completion: nil)
                } else {
                  println("No storyboard")
                }
              } else {
                println("No root view controller")
              }
            } else {
              println("No window")
            }
          }
        }
      } else {
        println("Failed to get trip id")
      }
    }
  }

The storyboard is constructed that when the app first opens, it opens to the LoginViewController, which checks login state and redirects to a NavigationController containing a list of trips. From the list, a user can tap a trip to open the TripViewController (see screenshot).

When I run my app and test tapping on a push notification, the app loads the trip list and I see the following log in my console:

2015-09-04 09:50:07.158 GoDriver[883:377922] Warning: Attempt to present <GoDriver.TripViewController: 0x15f5b260> on <GoDriver.LoginViewController: 0x15d910e0> whose view is not in the window hierarchy!

Do I have to load up my Navigation Controller and populate it with the TripViewController?

like image 868
Dave Long Avatar asked Feb 15 '26 09:02

Dave Long


1 Answers

If you are using UIStoryBoard and using the initialViewController, iOS automatically does the needful i.e loads it up, creates navigationController if needed and loads it to window.

However in this case you will need to do this bit manually. You would need to create a UINavigationController, populate it with your TripViewController and hook it with UIWindow.

like image 188
Shamas S Avatar answered Feb 16 '26 23:02

Shamas S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!