无法摆脱致命异常:NSInternalInconsistencyException错误

Fatal Exception: NSInternalInconsistencyException
Application windows are expected to have a root view controller at the end of application launch
-[UIApplication _runWithMainScene:transitionContext:completion:]

Crash Report

Fatal Exception: NSInternalInconsistencyException
    0  ???                            0x184066d8c (Missing)
    1  ???                            0x1832205ec (Missing)
    2  ???                            0x184066bf8 (Missing)
    3  ???                            0x184a56fa0 (Missing)
    4  UIKit                          0x18dc42a80 -[UIApplication _runWithMainScene:transitionContext:completion:]
    5  UIKit                          0x18e272b1c __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke
    6  UIKit                          0x18dc41dd0 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:]
    7  UIKit                          0x18dc41c6c -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]
    8  UIKit                          0x18dc40afc -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:]
    9  UIKit                          0x18e8d684c __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke
    10 UIKit                          0x18dc401ec -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]
    11 UIKit                          0x18e6bbac8 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke
    12 UIKit                          0x18e809bf8 _performActionsWithDelayForTransitionContext
    13 UIKit                          0x18dc3fc0c -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]
    14 UIKit                          0x18dc3f5a8 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:]
    15 UIKit                          0x18dc3c5e0 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:]
    16 UIKit                          0x18dc3c330 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:]

崩溃报告 Headers 是

UIKit
-[UIApplication _runWithMainScene:transitionContext:completion:]

applicationDidFinishLaunching

if (launchOptions == nil) {
    if (TegKeychain.get("ISLOGGEDIN") == "1") {
        heartbeat()
        let initialViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeTab")
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    } else {
        let initialViewController = storyboard.instantiateViewController(withIdentifier: "Entrance")
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    }
} else {
    if let notifications = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
        heartbeat()

        if let type = notifications["type"] as? String
        {
            if type == "msg" {
                if let userID = notifications["userID"] as? String {
                    userDefaults.set(userID, forKey: "goToChat")
                }

                let initialViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeTab")
                self.window?.rootViewController = initialViewController
                self.window?.makeKeyAndVisible()

                if let tabBarController = self.window!.rootViewController as? UITabBarController {
                    tabBarController.selectedIndex = 3
                }
            }

            if type == "follow" {
                if let userID = notifications["userID"] as? String {
                    userDefaults.set(userID, forKey: "goToProfile")
                }
                let initialViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeTab")
                self.window?.rootViewController = initialViewController
                self.window?.makeKeyAndVisible()

                if let tabBarController = self.window!.rootViewController as? UITabBarController {
                    tabBarController.selectedIndex = 0
                }
            }

            if type == "attend" {
                if let eventID = notifications["eventID"] as? String {
                    userDefaults.set(eventID, forKey: "goToEvent")
                }
                let initialViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeTab")
                self.window?.rootViewController = initialViewController
                self.window?.makeKeyAndVisible()

                if let tabBarController = self.window!.rootViewController as? UITabBarController {
                    tabBarController.selectedIndex = 0
                }
            }
        }
    }

    if let _ = launchOptions?[UIApplicationLaunchOptionsKey.location] {
        startSignificationLocation()
    }
}

什么是崩溃,我怎么能摆脱它?

回答(1)

2 years ago

applicationDidFinishLaunching 完成之前,您的应用程序委托未设置主UIWindow的 rootViewController 属性 . 这通常是在所有Xcode模板中为您设置的 . 如果您创建自己的窗口,请确保立即设置其根视图控制器 .

正如rmaddy所说,在你的代码中,如果设置了 launchOptions ,但是 launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] 返回nil,则永远不会设置 rootViewController . 如果 notifications["type"] as? String 为nil,则永远不会设置 rootViewController . 如果类型不是您期望的三个值,则永远不会设置 rootViewController .

必须在此方法完成之前设置 rootViewController ,否则应用程序将崩溃 . 您需要涵盖应用程序可以启动的所有可能方式 . 没有应用程序可以启动的每种可能方式的承诺列表,因此您不期望(或者"the app crashes"是您在这种情况下请求的行为) .