我正在尝试在收到远程通知后在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,但仍然崩溃。有人可以建议吗?
答案 0 :(得分:2)
评论这2行
// window = UIWindow(frame: UIScreen.main.bounds)
// window?.makeKeyAndVisible()
因为覆盖window属性会破坏storyboard的初始化(使rootVC为nil),而return true
窗口必须有一个