问题描述
因此出于某种原因,从 AppDelegate
实例化时总是会出现问题。我有一个带有 UNNotificationAction
的通知,我在 didReceiveResponse()
方法中处理操作选择。
功能如下:
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
let storyboard = UIStoryboard(name: "Main",bundle: nil)
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
if response.actionIdentifier == "viewNewEvent" {
let dashVC: UIViewController = storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
let navDashVC = UINavigationController(rootViewController: dashVC)
self.window?.rootViewController = navDashVC
self.window?.makeKeyAndVisible()
}
print(userInfo)
print("didReceiveResponse called")
completionHandler()
}
当我选择通知中的操作时,if
语句中的代码块永远不会运行。起初,我强行打开所有东西,我收到一条错误消息,说 viewController 为零,现在当我运行并选择通知中的操作时,什么也没有发生。
viewController 怎么可能为零?我遇到了类似的问题,并决定在 SceneDelegate
中尝试它,它确实有效,但有太多错误需要我最终修复。
在这种情况下,我是否最好使用 UIScene
和 UIWindowScene
而不是仅使用 UIWindow
来尝试实例化正确的 VC?或者有解决办法吗?
解决方法
好的,经过几个小时的研究,我意识到这不能再在 AppDelegate
中完成,我现在必须使用 SceneDelegate 来处理 UIWindow
和 {{ 1}}