繁体   English   中英

如何从Appdelegate打开Viewcontroller?

[英]How to open Viewcontroller from Appdelegate?

我使用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;

在您的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")

当您在didReceiveRemoteNotification委托中接收通知时,然后将函数推入pushview到nextviewcontroller。

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
    }
}

这里是

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 {
            }
        }
    }

不过,您仍然遇到任何问题,请让我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM