gpt4 book ai didi

ios - 如果应用程序通过推送通知打开,设置 Root View Controller 会有所不同吗?

转载 作者:行者123 更新时间:2023-11-30 12:04:37 24 4
gpt4 key购买 nike

我的应用程序有默认启动,但在某些情况下,我希望应用程序通过推送通知打开,该通知提供标识要加载的页面的有效负载。

我遇到的问题是有效负载逻辑是在推送通知的 didRecieve 响应中处理的,而且我还在 didFinishLaunchingWithOptions 中处理应用程序的启动。

我怎样才能阻止这两个冲突,因为目前它似乎使应用程序崩溃。

本质上,我想根据通知中的有效负载在启动时更改根目录。

目前我有:

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// map
let conferenceID = userInfo["cid"] as! String
self.pushNotifiedEventID = conferenceID
self.defaultEvent = conferenceID
if let p = userInfo["p"] as? [String : String] {
let pageID = p["id"]!
let title = p["title"]!
let type = p["type"]!
}
completionHandler()
let hostViewController = HostViewController()
self.window?.rootViewController = hostViewController
}

还有:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
center.delegate = self
center.requestAuthorization(options: [.badge, .alert , .sound]) { (granted, error) in
if granted {
DispatchQueue.main.async(execute: {
UIApplication.shared.registerForRemoteNotifications()
})
}
}
} else {
let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
let setting = UIUserNotificationSettings(types: type, categories: nil)
UIApplication.shared.registerUserNotificationSettings(setting)
UIApplication.shared.registerForRemoteNotifications()
}
// root VC init + push
window = UIWindow()
let loginViewController = LoginViewController.init(loginView: LoginView(frame: UIScreen.main.bounds))
self.navigationController = UINavigationController(rootViewController: loginViewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}

最佳答案

在您的 AppDelegate 类中,创建一个方法:

func configureRootView(isComingFromNotification: Bool) {
if isComingFromNotification {
self.window?.rootViewController = HostViewController()
}
else {
let loginViewController = LoginViewController.init(loginView: LoginView(frame: UIScreen.main.bounds))
self.navigationController = UINavigationController(rootViewController: loginViewController)
window?.rootViewController = navigationController
}
}

您可以从 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler finishHandler: @escaping () -> Void)func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 方法。

关于ios - 如果应用程序通过推送通知打开,设置 Root View Controller 会有所不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812591/

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