代码之家  ›  专栏  ›  技术社区  ›  Utku Dalmaz

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

  •  -1
  • Utku Dalmaz  · 技术社区  · 6 年前
    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:]
    

    应用程序完成启动

    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 回复  |  直到 6 年前
        1
  •  1
  •   Rob Napier    6 年前

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

    正如rmaddy所说,在你的代码中,如果 launchOptions launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] 那么返回nil 根视图控制器 永远不会被设定。如果 notifications["type"] as? String 是零, 根视图控制器 永远不会被设定。如果类型不是您期望的三个值, 根视图控制器 永远不会被设定。

    根视图控制器 必须在此方法完成之前设置,否则应用程序将崩溃。你需要涵盖应用程序启动的所有可能方式。应用程序启动的所有可能方式都没有一个承诺的列表,因此当它以您不期望的方式启动时,您将需要某种回退(否则“应用程序崩溃”就是您在这种情况下请求的行为)。