是否可以在 SceneDelegate

问题描述

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

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

我在 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 中执行此类操作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...