0
votes

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

3
Are you specifying storyboard identifier in Storyboard of HomePageViewController controller - Aditya
Yeah I am specifying the id in the storyboard - Prabu Raj
can you try this once 'UIStoryboard(name: "Main", bundle: Bundle.main)' - Aditya
I fixed the black view problem. The problem is the navigation controller is still nil in the secondView Controller.@Aditya - Prabu Raj
Great .. did you have embedded navigation controller separately to second VC. - Aditya

3 Answers

1
votes

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
1
votes

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.

0
votes

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
    }