【发布时间】:2021-06-05 05:44:50
【问题描述】:
所以由于某种原因,从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 为 nil,现在当我运行并选择通知中的操作时,什么也没有发生。
viewController 怎么可能是 nil?我遇到了类似的情况,并决定在SceneDelegate 中尝试它,它确实有效,但有很多错误需要我最终修复。
在这种情况下,我最好使用UIScene 和UIWindowScene 而不是仅使用UIWindow 来尝试实例化正确的VC?或者有解决办法吗?
【问题讨论】:
标签: swift storyboard apple-push-notifications appdelegate uiscenedelegate