Search code examples
iosvlc

Redirect to RootViewController ios


Im modifying an older ios app which has the mainscreen setup through :

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
rootViewController = [UITabBarController new];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
[self setup];

How can i programatically redirect back to that screen? Im adding a login screen and need to know how to redirect after successful login.

Thanks


Solution

  • #define kUserAuthChangedNotification  @"kUserAuthStatusChanged"
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [self changeRootControllerWithIsUserSignIn:NO];//You must send user sign in or not
        [self.window makeKeyAndVisible];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userAuthStatusChanged) name:kUserAuthChangedNotification object:nil];
    }
    
    #pragma mark helper methods
    
    - (void)userAuthStatusChanged {
        [self changeRootControllerWithIsUserSignIn:YES];//You must send user sign in or not
    }
    
    - (void)changeRootControllerWithIsUserSignIn:(BOOL)isSignIn {
        if(isSignIn){ 
            rootViewController = [UITabBarController new];
            self.window.rootViewController = rootViewController;
            [self setup];
        } else {
            YourLoginViewController * ctrl = [YourLoginViewController new];
            self.window.rootViewController = ctrl;
        }
    }
    

    When successful login Or log out you must call:

    [[NSNotificationCenter defaultCenter] postNotificationName:kUserAuthChangedNotification object:nil];