ios - 应用程序被杀死时点击推送通知后应用程序崩溃
问题描述
我正在尝试在收到远程通知后在我的 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
}
}
我已经按照这篇文章检查了launchOptions,但它仍然崩溃。接下来我可以尝试什么?
解决方案
评论这两行
// window = UIWindow(frame: UIScreen.main.bounds)
// window?.makeKeyAndVisible()
因为覆盖窗口属性会破坏情节提要的初始化(使 rootVC 为零)并且在return true
窗口必须有一个之前
推荐阅读
- postgresql - Powershell编码后如何转换postgresql转储?
- python - 如何停止下载提示 - Selenium Python
- python - Python Pandas for 循环遍历文件夹 concat numpy 数组到文件夹中的每个 xlsx 文件
- r - 将多条回归线添加到折线图 R
- puppeteer - 如何使用 puppeteer 选择表弟的内部文本
- javascript - 根据另一个数组修改一个数组
- windows - 使 IIS 使用 ::1 获取默认网站,而不仅仅是使用 127.0.0.1
- swift - 我可以使用与 Equatable 不同的属性来遵守 Hashable 吗?
- c# - C# Lambda 中的自定义 AWS X-Ray 跟踪
- python - Scrapy runspider 命令行参数