如何解决是否可以在 SceneDelegate
我的目标是让用户在点击通知操作时直接转到 viewController。我知道与配置 UIWindow
和 UIScene
相关的所有事情都已转移到场景委托中。所以我在 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 举报,一经查实,本站将立刻删除。