打开通知到特定的视图

我正在使用推送通知在Swift上开发一个应用程序。 到目前为止,我的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") } } } 

故事板的构build是,当应用第一次打开时,它会打开LoginViewController,它会检查login状态并redirect到包含旅行列表的NavigationController。 从列表中,用户可以点击旅行打开TripViewController(见截图 )。

当我运行我的应用程序并testing点击推送通知时,应用程序将加载旅程列表,并在控制台中看到以下日志:

 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! 

我是否必须加载我的导航控制器并使用TripViewController填充它?

如果你正在使用UIStoryBoard并使用initialViewController ,则iOS会自动执行需要的操作,即加载它,根据需要创buildnavigationController并将其加载到窗口中。

但是在这种情况下,您将需要手动执行此操作。 你将需要创build一个UINavigationController,用你的TripViewController填充它,并用UIWindow挂钩它。