【问题标题】:How to open Viewcontroller from Appdelegate?如何从 Appdelegate 打开 Viewcontroller?
【发布时间】:2019-02-15 04:30:35
【问题描述】:

我使用firebase向ios设备发送消息,我调试,我在func的Appdelegate中收到了数据有效负载

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

我想做如何根据那个数据打开不同的视图控制器,也就是说当我点击消息时,我会去对应的视图控制器。 我在 Appdelegate 中使用了下面的代码但失败了

    let sb = UIStoryboard(name: "Main", bundle: nil)
    let otherVC = sb.instantiateViewController(withIdentifier: "UserNotLoginViewController") as! UserNotLoginViewController
    self.window?.rootViewController = otherVC;

【问题讨论】:

  • 检查您的 otherVC 对象以及它不应该为 nil 的窗口对象!哇,你是在调用 show VC code 吗?
  • 如果 nil 则应用程序将崩溃,但我的应用程序只是没有打开不同的 Viewcontrollers
  • nil 并不意味着应用程序总是会崩溃!

标签: ios swift firebase push-notification firebase-cloud-messaging


【解决方案1】:

当您在 didReceiveRemoteNotification 委托中收到通知时,然后调用该函数将视图推送到下一个视图控制器。

func application(_ application: UIApplication, didReceiveRemoteNotification 
  data: [AnyHashable : Any]) {
    let state: UIApplicationState = UIApplication.shared.applicationState
    if state == .background {
        // background
        pushToPage(data: data)
    }
}

   func pushToPage(data:[AnyHashable : Any]){
     if  let appDelegate =  UIApplication.shared.delegate as? AppDelegate,
      let window = appDelegate.window {
      let storyBoard : UIStoryboard = UIStoryboard(name: "Main",  bundle:nil)
      let nextViewController = 
         storyBoard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
       window.rootViewController = nextViewController
    }
}

【讨论】:

  • 我尝试了你的方法,但除了默认的 Viewcontroller 之外,仍然没有打开不同的 Viewcontroller
  • 当您点击通知时,此调用是否为 didreceiveRemoteNotification 委托?
  • 请查看在此链接上创建的演示。我已经使用了本地通知。点击按钮接收通知。收到通知后,从后台删除应用程序。点击通知。它将转到指定屏幕 -drive.google.com/open?id=1HJRcfmytWJKuHbDt4ko-RPmWYaSysNcr
【解决方案2】:

在你的 appDelegate 中声明这个函数,然后用它来改变 rootViewController。

 public func makeRootVC(vcName : String) {
    let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
    let navigation = UINavigationController(rootViewController: vc)
    navigation.navigationBar.isHidden = true

    self.window?.rootViewController = navigation
}

用法:

self.makeRootVC("YourViewControllerStoryboardID")

【讨论】:

    【解决方案3】:

    这里是

    func handlePushNotification(userInfo: [String: Any]) {
    
            guard let notificationType = userInfo["nt"] as? String else {
                return
            }
    
            if notificationType.toInt() == 1 {
                self.navigateToViewController1()
            } else if notificationType.toInt() == 2 {
                self.navigateToViewController2()
            }
    
        }
    

    对于导航,你可以使用下面这个函数

    fileprivate func navigateToViewController1() {
            if let rootViewController = self.window?.rootViewController as? UINavigationController {
    
                if let _ = rootViewController.topViewController as? VC1 {
                    let vc = AppStoryboard.Main.viewController(viewControllerClass: VC3.self)
                    rootViewController.pushViewController(vc, animated: true)
                }
            }
        }
    
        fileprivate func navigateToViewController2() {
    
            if let rootViewController = self.window?.rootViewController as? UINavigationController {
    
                if let homeVC = rootViewController.topViewController as? VC2 {
                }
            }
        }
    

    不过,您遇到任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      相关资源
      最近更新 更多