1
votes

I am trying to Display Selected ViewController based on user has Logged in inside the app or not.if user has logged in then it should display HomeViewController, if not then it should display InitialViewController.My Initial StoryBoard is MainStoryBoard and LoginVC are in InitialStoryBoard. Below is my sample code and struggling lot to fix this issue.can anybody help to fix this issue .

/- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions* /

BOOL isLoggedIn = [[NSUserDefaults standardUserDefaults]boolForKey:@"isLoggedIn"];
NSLog(isLoggedIn ? @"Yes" : @"No");
NSLog([[NSUserDefaults standardUserDefaults]boolForKey:@"isLoggedIn"] ? @"Yes" : @"No"); 
if (!isLoggedIn) {
    NSString *device2;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        device2 = @"InitialStoryboard_iPad";
    }
    else
    {
        device2 = @"InitialStoryboard_iPhone";
    }
    NSLog(@"device :%@",device2);
    UIStoryboard *storyboard2 = [UIStoryboard storyboardWithName:device2 bundle:nil];
    InitialVC *initialController = [storyboard2 instantiateInitialViewController];
   //        [(UINavigationController *)self.window.rootViewController presentViewController:initialController animated:YES completion:nil];
   //        [(UINavigationController *)self.window.rootViewController pushViewController:initialController animated:NO];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = initialController;
    [self.window makeKeyAndVisible];
}

If Login is Successful then i am not able to dismissCurrentVC as Initial is RootVC.If login is Successful then it should make DismissVC if MainStoryBoard is Initial Storyboard.or login Successful should make HomeVC PresentVCModally if InitialStoryBoard is InitialStoryboard.Hope you can understand my Issue.

Thanks in Advance

1
So where is your issue? On initial launch of the app, or when user logs in you want to dismiss rootViewController and reorder viewController stack??Pancho

1 Answers

0
votes

Instead of using different storyboards, I suggest you use only one storyboard (for each device) which contains the RootVC and LoginVC. (RootVC is default initial VC)

if([[NSUserDefaults standardUserDefaults] stringForKey:@"userName"].length){
    NSLog(@"User Logged In, Show Root VC");
    //RootVC is Default.
}else{
    NSLog(@"User Not Logged In, Show Login NVC");
    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"LoginNVC"];
}

After user logged in, you can then switch back to rootVC:

- (void)loginAndShowRootVC{
    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"RootVCID"];
}