1
votes

Comment ouvrir Viewcontroller depuis Appdelegate?

J'utilise Firebase pour envoyer un message à un appareil ios, je débogue, j'ai reçu des données utiles dans Appdelegate dans func

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

Je veux faire comment ouvrir différents contrôleurs de vue en fonction de ces données, ce qui signifie que lorsque je clique sur le message, je vais accéder aux contrôleurs de vue correspondants. J'ai utilisé le code ci-dessous dans Appdelegate mais j'ai échoué

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


3 commentaires

vérifiez votre objet otherVC également l'objet window, il ne doit pas être nul! Wow vous appelez le code show VC?


Si nul, l'application plante, mais mon application n'ouvre tout simplement pas différents Viewcontrollers


nul ne signifie pas que l'application plantera toujours !!


3 Réponses :


0
votes

Déclarez cette fonction dans votre appDelegate, puis utilisez-la pour changer rootViewController.

self.makeRootVC("YourViewControllerStoryboardID")

utilisation :

 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
}


0 commentaires

1
votes

lorsque vous recevez une notification dans le délégué didReceiveRemoteNotification, appelez la fonction pour pousser la vue vers 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
    }
}


3 commentaires

J'ai essayé votre chemin mais je n'ai toujours pas ouvert différents Viewcontroller, sauf le Viewcontroller par défaut


lorsque vous appuyez sur la notification, cet appel a-t-il été reçu par un délégué de notification à distance?


veuillez consulter la démo créée sur ce lien. J'ai utilisé la notification locale. cliquez sur le bouton pour recevoir une notification. une fois la notification reçue, supprimez l'application de l'arrière-plan. appuyez sur la notification. il ira à l'écran désigné - drive.google.com/open?id=1HJRcfmytWJKuHbko-



0
votes

Le voici

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

Et pour la navigation, vous pouvez utiliser cette fonction ci-dessous

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

    }

Pourtant, vous rencontrez un problème alors s'il vous plaît faites le moi savoir.


0 commentaires