0

I have few view controllers embedded in UINavigationController. The first view controller is login page. The second view controller is the home page. I want initialview controller as second view controller when the user is already logged in.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
{
    // Override point for customization after application launch.
    if let data = Locksmith.loadDataForUserAccount(userAccount: "someString")
    {
        if let userAccessToken = data["accessToken"]
        {
            if (userAccessToken as! String) != ""
            {
                let initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
                loginResponse =  LoginResponse()
                loginResponse?.UserAccessToken = userAccessToken as? String
                self.window = UIWindow(frame: UIScreen.main.bounds)
                self.window?.rootViewController =  initialViewController
                self.window?.makeKeyAndVisible()
            }
        }
    }
    return true
}

The problem is the subsequent view controllers are not embedded in navigation controller. Since it is not embedded in navigation controller I am not able to goback from one view controller to the other.

enter image description here

Community
  • 1
  • 1
Prabu Raj
  • 577
  • 1
  • 6
  • 11
  • Are you specifying storyboard identifier in Storyboard of HomePageViewController controller – Aditya Sep 15 '17 at 11:23
  • Yeah I am specifying the id in the storyboard – Prabu Raj Sep 15 '17 at 11:24
  • can you try this once 'UIStoryboard(name: "Main", bundle: Bundle.main)' – Aditya Sep 15 '17 at 11:28
  • I fixed the black view problem. The problem is the navigation controller is still nil in the secondView Controller.@Aditya – Prabu Raj Sep 15 '17 at 11:38
  • Great .. did you have embedded navigation controller separately to second VC. – Aditya Sep 15 '17 at 11:40
  • Check the screenshot. First one is the navigation controller, the second one is the login view controller and the third one is the home view controller.While running the app home view conroller and other view conrollers are not embedded in navigation conroller. – Prabu Raj Sep 15 '17 at 11:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154531/discussion-between-prabu-raj-and-aditya). – Prabu Raj Sep 15 '17 at 11:44

3 Answers3

1

Add this in App delegate

first check user already login or not, if login then execute this code

let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
 let redViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController
 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
 appDelegate.window?.rootViewController = redViewController
Gaurav Saini
  • 177
  • 1
  • 1
  • 8
1

Use this in App Delegate and add an extension of UIStoryboard.

func checkForAlreadyLogin() {
    let dashBoardScreen = UIStoryboard.dashBoardScreen()
    let loginController = UIStoryboard.loginController()

    if UserDefaults.standard.bool(forKey: UserDefaultValues().LOGINSTATUS){
        self.window!.rootViewController = dashBoardScreen

    }else {
        self.window!.rootViewController = loginController
    }

}


public extension UIStoryboard {
   class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: Bundle.main) }

   class func dashBoardScreen() -> HomeViewController?{
       return mainStoryboard().instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
   }

   internal class func loginController() -> LoginViewController?{
       return mainStoryboard().instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
   }
}

Just call this checkForAlreadyLogin() method in 'didFinishLaunchingWithOptions'.

Also remember to set StoryboardID in the storyboard for each viewController.

Aditya
  • 783
  • 1
  • 8
  • 25
0

This code did the trick for me.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if let data = Locksmith.loadDataForUserAccount(userAccount: "someString")
        {
            if let userAccessToken = data["accessToken"]
            {
                if (userAccessToken as! String) != ""
                {
                    let initialViewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
                    let navigationController = self.window?.rootViewController as! UINavigationController
                    navigationController.pushViewController(initialViewController, animated: true)
                }
            }
        }
        return true
    }
Prabu Raj
  • 577
  • 1
  • 6
  • 11