1

I am trying to switch my root ViewController to my HomeViewController when a notification is tapped containing the "Home" category. Since the addition of SceneDelegate, I've hit a wall with transitioning the ViewController from AppDelegate.

My AppDelegate, using storyboard Swift 5 and Xcode 11.6

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("Handle push from background or closed")
        print("user clicked on the notification")
        let userInfo = response.notification.request.content.userInfo
        let categoryValue = userInfo["category"] as? String ?? "0"
        if categoryValue == "Home"
        {
            print("Switching to Home!")

            //change ViewController here

            }
            
        }
    }

Thank you for your help!

Codelinsky
  • 17
  • 3
  • Unclear what the problem is. Does your code run? Does "Switching to Home" print? Or is the problem that you do not know how to talk to the scene delegate from the app delegate? – matt Sep 06 '20 at 06:07
  • Yes the code runs fine, the problem is switching view controllers programmatically. Something like this is what I need but I can't find a way to make it work from appdelegate: self.view.window?.rootViewController = homeViewController self.view.window?.makeKeyAndVisible() The problem is talking to the scene delegate from app delegate. – Codelinsky Sep 06 '20 at 06:12

1 Answers1

0

So the problem is how to talk to the scene delegate?

The application is UIApplication.shared. The application has connectedScenes. Take the first one and ask for its delegate. Cast that down to SceneDelegate and talk to it.

Or if you want to manipulate the window directly (I don’t advise it), ask the application for its windows. Again, take the first one.

matt
  • 515,959
  • 87
  • 875
  • 1,141