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

时间:2018-09-10 15:21:51

标签: ios swift crash

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

崩溃报告

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:]

崩溃报告标题为

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 个答案:

答案 0 :(得分:1)

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

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

必须先设置

rootViewController,然后该方法才能崩溃。您需要涵盖应用程序启动的所有可能方式。没有可承诺的每种应用程序启动方式的清单,因此,当您以一种意外的方式启动应用程序时,您将需要某种后备(否则,“应用程序崩溃”就是您的行为)在这种情况下重新请求。