在Swift 5中点击通知时切换ViewController

问题描述

当尝试点击包含“主页”类别的通知时,我试图将根ViewController切换到HomeViewController。自添加SceneDelegate以来,我从AppDelegate过渡到ViewController遇到了麻烦。

我的AppDelegate,使用情节提要Swift 5和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

            }
            
        }
    }

谢谢您的帮助!

解决方法

所以问题是如何与现场代表交谈?

应用程序为UIApplication.shared。该应用程序具有connectedScenes。选择第一个,并索取其delegate。将其投放到SceneDelegate并与之交谈。

或者,如果您想直接操作窗口(我不建议这样做),请向应用程序索要其windows。再次,选择第一个。