Marking
Marking

Reputation: 184

Setting root view controller is not working?

My code is

#import "AppDelegate"

in viewDidLoad

self.delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

After getting the response

//If server response is success make HomeViewController as Root VC
if ([[[_response objectForKey:@"Response"] objectForKey:@"status"]  isEqualToString:@"SUCCESS"]) {

dispatch_async(dispatch_get_main_queue(), ^{

//Make root view controller
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; //My storyboard name is Main.storyboard
HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"HVC"];
self.delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];
[self.delegate.window makeKeyAndVisible];

});
} 

But it's not working...

Upvotes: 0

Views: 2712

Answers (4)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16446

What you are trying to implement is auto login functionality (if user already logged in then no need for login page). What you are doing is correct but you also need to managed that thing from AppDelegate too.
You need to add one line to code after setting root view controller is

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isUserLoggedIn"]; 
        // PUT this line after  [self.delegate.window makeKeyAndVisible];

You can create method in AppDelegate which monitor a boolean variable from UserDefault. and navigate to vc1 -> vc2

Put it in Appdelegate and call it from didFinishLuanchWithOptions

- (void) checkLoginStatusAndProceed {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isUserLoggedIn"] && [[NSUserDefaults standardUserDefaults] boolForKey:@"isUserLoggedIn"] == YES) {
        // Navigate to HomeViewController
        //Make root view controller
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; //My storyboard name is Main.storyboard
        HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"HVC"];
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];
       [self.window makeKeyAndVisible];
    }

}

Upvotes: 1

Vishal Sonawane
Vishal Sonawane

Reputation: 2693

As per your requirements, you need to maintain a flag may be in UserDefaults.

NOTE: Its just pseudo code, as I am not at my desk with laptop

Follow steps like this:

  1. Create user defaults object and save flag e.g isFirstLaunch = false
  2. After setting your HomeVC as root, change this flag to true .
  3. In AppDelegate in didFinishLaunchingWithOptions, check for this flag again like

    if UserDefaults.standard.bool(for:"isFirstLaunch"){ //Show Login }else{ //Show Home }

Upvotes: 1

Mahendra
Mahendra

Reputation: 8914

In your LoginViewController you need to keep track of the user login status that user is successfully logged in. For that you can store a bool var in UserDefaults like...

UserDefaults.standard.setValue(true, forKey: "isUserLoggedIn")
UserDefaults.standard.synchronize()

So when ever app is relaunched then you need to check that user is already logged in or not....and based on that you can show desired screen...

if let isLoggedIn = UserDefaults.standard.value(forKey: "isUserLoggedIn") as? Bool, isLoggedIn == true {

   //Make root view controller
   UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
   HomeViewController * hvc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"HVC"];
   self.delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:hvc];

} else {
    //show login view controller as user is not yet logged in
}

Upvotes: 1

Anuraj
Anuraj

Reputation: 1240

SWIFT Code Implement Two methods in Appdelegate Class like this :-

    class AppDelegate: UIResponder, UIApplicationDelegate {
        var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

            application.statusBarStyle = .lightContent
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.makeKeyAndVisible()

            return true
        }
        func showTabBar()  {

            let tabBarContoller = UIStoryboard(name: OTPStoryBoards.Booking, bundle: nil).instantiateViewController(withIdentifier: OTPViewControllers.TabBarController) as! UITabBarController
            self.window?.rootViewController = tabBarContoller
        }

        func showLogin()  {
            let loginVC = UIStoryboard(name: OTPStoryBoards.Authentication, bundle: nil).instantiateViewController(withIdentifier: OTPViewControllers.LoginVC) as! OTPLoginViewController
            let navigationControler = UINavigationController(rootViewController: loginVC)
            self.window?.rootViewController = navigationControler
        }
}

And whenever you want to change root call these methods

let APP_DELEGATE    = UIApplication.shared.delegate as! AppDelegate

  APP_DELEGATE.showLogin() or APP_DELEGATE.showTabBar() 

Upvotes: 1

Related Questions