0

我的目标是让用户在点击通知操作时直接进入 viewController。我知道所有与配置有关的事情UIWindow都已UIScene移至场景委托。所以我didReceiveResponse在我的中添加了SceneDelegate作为扩展名。

我现在设置应用程序的方式是,当用户首次启动应用程序或终止应用程序并重新启动它时,他们会根据用户类型重定向到正确的 viewController。

我在willConnectTo()方法中这样做:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    
    let window = UIWindow(windowScene: windowScene)
    self.window = window
          let auth = Auth.auth()


       auth.addStateDidChangeListener { (_, user) in
              if let usersignedin = user {
                  print(usersignedin.email ?? "Can't get user email")

                  db.document("student_users/\(usersignedin.uid)").getDocument { (docSnapshot, error) in
                      if error != nil {
                          print("There was an error redirecting the signed in user")
                      } else {
                          let docSnap = docSnapshot?.exists
                          guard docSnap! else {
                              let alreadyLoggedInAsASchoolViewController = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.SchoolEventDashboardStoryboardID) as! SchoolTableViewController
                              let navigationizedSchoolVC = UINavigationController(rootViewController: alreadyLoggedInAsASchoolViewController)
                              self.window!.rootViewController = navigationizedSchoolVC
                              self.window!.makeKeyAndVisible()
                              return
                          }

                          let alreadyLoggedInAsAStudentViewController = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
                          let navigationizedVC = UINavigationController(rootViewController: alreadyLoggedInAsAStudentViewController)
                          self.window!.rootViewController = navigationizedVC
                          self.window!.makeKeyAndVisible()

                      }
                  }



              } else {
                  print("No user is signed in")
                  let notLoggedInAtAll = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.GothereMainMenuStoryboardID) as! GothereMainMenuViewController
                  let navMainMenu = UINavigationController(rootViewController: notLoggedInAtAll)
                  self.window!.rootViewController = navMainMenu
                  self.window!.makeKeyAndVisible()
              }
          }
}

这很好用,我最初有它,sceneWillEnterForeground()但意识到如果他们要切换应用程序并回到他们正在做的事情,它会回到主屏幕,这会令人沮丧。因此,我对此的解决方案是将其放入willConnectTo,然后当收到通知时无论他们在做什么,他们都可以被引导到该主屏幕。

在我的didReceiveResponse,我有这段代码:

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    center.delegate = self
    
    if response.actionIdentifier == "viewNewEvent" {
        let alreadyLoggedInAsAStudentViewController = storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
        let navigationizedVC = UINavigationController(rootViewController: alreadyLoggedInAsAStudentViewController)
        self.window!.rootViewController = navigationizedVC
        self.window!.makeKeyAndVisible()
    }
    completionHandler()
}

当我运行它并使用通知对其进行测试时,我认为这会起作用,但它最终只是转到应用程序进入后台时用户最后一次使用的 viewController。我尝试添加guard语句和其他代码行,就像我在willConnectTo(). 我有一个全局window变量和一个变量,storyboard所以这就是为什么函数没有错误地拾取它的原因,因为其他变量都在willConnectTo(). 为了完成这项工作,我是否缺少或需要删除某些东西,或者根本不可能在SceneDelegate?

4

0 回答 0