UIApplication.sharedApplication (). KeyWindow is a zero inside a structure test.

I am trying to check (using the XCTest framework) the view controllers that are inside the frame (dynamic library). I am having a problem with the UIApplication.sharedApplication () property. KeyWindow, which is zero. I have the following code:

override func setUp() {
  super.setUp()

  let storyboard = UIStoryboard(name: STORYBOARD_NAME_REPORT,
                                bundle: BundleUtil.getCurrentBundle())
  let chatViewController = storyboard.instantiateViewControllerWithIdentifier(VIEW_CONTROLLER_ID_TIP_CHAT) as UIViewController

  let navigationController = UINavigationController()
  let viewControllers = [chatViewController]
  navigationController.viewControllers = viewControllers

  // PROBLEM: The following causes a crash because keyWindow is nil.
  UIApplication.sharedApplication().keyWindow!.rootViewController = chatViewController

  // Force the view controller to be loaded.
  // Source: https://www.natashatherobot.com/ios-testing-view-controllers-swift/
  let _ = navigationController.view
  let _ = chatViewController.view
}

Any ideas on how to properly initialize the keywindow property?

+4
source share

All Articles