Swift didReceiveRemoteNotification - 无论app用户位于何处,都可以导航到rootviewcontroller(现在有错误信息)

业无高卑志当坚,男儿有求安得闲?这篇文章主要讲述Swift didReceiveRemoteNotification - 无论app用户位于何处,都可以导航到rootviewcontroller(现在有错误信息)相关的知识,希望能为你提供帮助。
在ios Swift应用程序中收到推送通知后,我想根据通知中的内容做两件事:

  1. 导航到屏幕(深层链接),所以我必须从rootviewcontroller导航到几个屏幕。
  2. 无论用户在应用程序中的哪个位置,都可以导航到rootviewcontroller。
第二个我认为是第一个的先决条件。
我知道我需要在这两个函数中放置代码:
  • didReceiveRemoteNotification
  • didReceiveLocalNotification
错误消息:'UIViewcontroller?'没有名为'navigationController'的成员
在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) }

答案【Swift didReceiveRemoteNotification - 无论app用户位于何处,都可以导航到rootviewcontroller(现在有错误信息)】在您的方法中尝试此代码:
var rootViewController = self.window!.rootViewController let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)var setViewController = mainStoryboard .instantiateViewControllerWithIdentifier("CurrentShows") as ViewController_CurrentShowsrootViewController .navigationController .popToViewController(setViewController, animated: false)

另一答案需要创建一个变量:var window:UIWindow
下面的代码在appdelegate中实现,在:func didBecomeActive()....
let storyboard = UIStoryboard(name: "Main", bundle: nil) let rootController = storyboard.instantiateViewControllerWithIdentifier("Login") as Login_ViewControllerself.window?.rootViewController?.dismissViewControllerAnimated(false, completion: { if self.window != nil { self.window!.rootViewController = rootController } })

另一答案我编写了一个简单的类,通过传递类类型,从一行代码中的任何位置导航到视图层次结构中的任何视图控制器,因此您将编写的代码也将与视图层次结构本身分离,例如:
Navigator.find(MyViewController.self)?.doSomethingSync() Navigator.navigate(to: MyViewController.self)?.doSomethingSync()

..或者你也可以在主线程上异步执行方法:
Navigator.navigate(to: MyViewController.self) { (MyViewControllerContainer, MyViewControllerInstance) in MyViewControllerInstance?.doSomethingAsync() }

这里是GitHub项目链接:https://github.com/oblq/Navigator

    推荐阅读