gpt4 book ai didi

ios - 推送通知 - 使用 SceneDelegate 在通知点击时推送 ViewController

转载 作者:行者123 更新时间:2023-11-29 05:11:08 32 4
gpt4 key购买 nike

在 iOS 13 之前,导航 Controller 和 Root View 是在 AppDelegate 中定义的。然而,在 iOS 13 中,Apple 引入了 SceneDelegate,它接管了这些 View 函数的处理。然而,AppDelegate 仍然处理诸如本地通知处理之类的事情。 See this answer for some code that outlines these changes for root views.

如果我希望在用户点击本地通知时推送 View ,我会在 AppDelegate 中执行如下操作:

extension AppDelegate: UNUserNotificationCenterDelegate {

var navigationController: UINavigationController?

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

if response.actionIdentifier == UNNotificationDefaultActionIdentifier { //User taps notification
let vc = MyViewController()
self.navigationController?.pushViewController(vc, animated: true)
}
}
}

但是,由于从 iOS 13 开始,我的项目的根导航 Controller 现在可以在 SceneDelegate 中定义,因此我似乎无法弄清楚如何在由 SceneDelegate 而不是 AppDelegate 管理的导航 Controller 中推送 View 。

最佳答案

SceneDelegate 可以这样处理通知响应:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
// rootViewController set up code
// say,
// let mainController = ViewController()
// let navigationController = UINavigationController(rootViewController: mainController)
// window.rootViewController = navigationController

// This is UNNotificationResponse
if let notificationResponse = connectionOptions.notificationResponse {
window.makeKeyAndVisible()
// do the pushing on your navigation controller
// navigationController.push()
return
}

window.makeKeyAndVisible()
}
}
}

关于ios - 推送通知 - 使用 SceneDelegate 在通知点击时推送 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59675593/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com