0

My goal is to check if there is a token in the keychain, if there isn't then simply show a login view controller screen. The problem right now, is that I get this error. I wrote this code in AppDelegate.swift

.LoginViewController: 0x7ff59b619820> on whose view is not in the window hierarchy!

Here's the code

   func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

     let keychain = Keychain(server: "https://app.herokuapp.com", protocolType: .HTTPS)

     if ((try? keychain.contains("token")) != nil) {
        showLoginScreen()
     } else {

     }
      return true
     }

    func showLoginScreen() -> Void {
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
        let rootViewController = self.window!.rootViewController
        rootViewController?.presentViewController(setViewController, animated: false, completion: nil)
    }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
airsoftFreak
  • 1,450
  • 5
  • 34
  • 64

2 Answers2

1

try this

add self.window.makeKeyAndVisible() before present

self.window.makeKeyAndVisible()

Update

   let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
      self.window.makeKeyAndVisible()
    self.window!.rootViewController.presentViewController(setViewController, animated: false, completion: nil)

for additional information see this

Community
  • 1
  • 1
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

By setting your view controller as UINavigationControllers rootviewcontroller and then adding navigationcontroller as Windows rootviewcontrollers will work check out the modified code below:

func showLoginScreen() -> Void {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as UIViewController
    let navigationController = UINavigationController(rootViewController: vc)
    self.window!.rootViewController = navigationController
    self.window!.makeKeyAndVisible()
}

If you like to switch between two viewcontrollers depending on the LoginStatus i suggest you to follow the below method:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
if LofinStatus == true{
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = mainStoryboard.instantiateViewControllerWithIdentifier("My_Offer") as UIViewController
        let navigationController = UINavigationController(rootViewController: vc)
        self.window!.rootViewController = navigationController
    }else{
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Login", bundle: nil)
        let vc = mainStoryboard.instantiateViewControllerWithIdentifier("loginView") as UIViewController
        let navigationController = UINavigationController(rootViewController: vc)
        self.window!.rootViewController = navigationController
    }
self.window!.makeKeyAndVisible()

NOTE: In the above code i had used two storyboards Main and Login It worked for me perfectly,give it a try and let me know the result.Thank You

Gokul G
  • 2,046
  • 15
  • 22