gpt4 book ai didi

ios - 应用程序被杀死时点击推送通知后应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 11:58:40 26 4
gpt4 key购买 nike

我正在尝试在收到远程通知后在我的 CustomTabBarController 中启动一个特定的 viewController。但不知何故,当应用程序被终止时,应用程序总是崩溃。

意思是说,终止应用程序 -> 收到推送通知 -> 点击通知 -> 应用程序启动并崩溃。当我从锁定屏幕点击通知时也会发生这种情况。

我可以在应用程序处于后台时执行,但不能在应用程序被终止时执行。到目前为止我的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()

...

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
guard let rootViewController = self.window?.rootViewController as? CustomTabBarController else {
return true
}
rootViewController.selectedIndex = 1

}

return true
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

if application.applicationState == .background || application.applicationState == .inactive {

guard let rootViewController = self.window?.rootViewController as? CustomTabBarController else {
return
}
rootViewController.selectedIndex = 1
}
}

我关注了这个 post检查 launchOptions,但它仍然崩溃。接下来我可以尝试什么?

最佳答案

注释这两行

// window = UIWindow(frame: UIScreen.main.bounds)
// window?.makeKeyAndVisible()

因为覆盖窗口属性会破坏 Storyboard的初始化(使 rootVC 为零)并且在返回 true 之前窗口必须有一个

关于ios - 应用程序被杀死时点击推送通知后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50427279/

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