簡體   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