0

Based on the condition user is logged into server or not shows different template. If user is not logged into application server need to show LoginViewController[UIViewController] else show the TabBarViewcontroller[UITabBarViewController].

Since I am using Storyboard first time. Can any one advice me to handle this type of condition within the storyboard?

Bharat Nakum
  • 647
  • 6
  • 18
kiran
  • 4,285
  • 7
  • 53
  • 98
  • possible duplicate of [Conditional Segue Using Storyboard](http://stackoverflow.com/questions/18947328/conditional-segue-using-storyboard) – Adam Bardon Aug 13 '15 at 07:42
  • possible duplicate of [Conditionally start at different places in storyboard from AppDelegate](http://stackoverflow.com/questions/8451975/conditionally-start-at-different-places-in-storyboard-from-appdelegate) – Razvan Aug 13 '15 at 13:24

4 Answers4

1

Create the window and desired initial view controller in the app delegate's application:didFinishLaunchingWithOptions: method:

self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];  
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
if ([self isUserLoggedIn]) {  
    // Show the dashboard
    self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"TabBarViewcontroller"];
} else {
    // Login
    self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
}
[self.window makeKeyAndVisible];
iAnurag
  • 9,286
  • 3
  • 31
  • 48
1

I did it as follows.

First, you create something called LoginViewController. It implements your login logic. Your root controller is UITabBarController.

Now in AppDelegate:

- (void) doLogin:(NSDictionary *)dict
{
        TransactionsViewController *transactions;
        BalancesViewController *balances;
        ProfileViewController *profile;
        UINavigationController *navi;
        UITabBarController *root;

        root = (UITabBarController *)self.window.rootViewController;

        navi = [root.viewControllers objectAtIndex:0];
        transactions = [navi.viewControllers objectAtIndex:0];
        transactions.delegate = self;

        navi = [root.viewControllers objectAtIndex:1];
        balances = [navi.viewControllers objectAtIndex:0];
        balances.delegate = self;

        navi = [root.viewControllers objectAtIndex:2];
        profile = [navi.viewControllers objectAtIndex:0];
        profile.delegate = self;

        [transactions loadDataFromLogin:dict];
        [balances loadDataFromLogin:dict];
        [profile loadDataFromLogin:dict];
}

- (void) showLoginView
{
        assert(loginController == nil);
        assert(activityView == nil);

        UITabBarController *tabbar = (UITabBarController *)self.window.rootViewController;
        loginController = [tabbar.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
        loginController.delegate = self;
        [tabbar presentModalViewController:loginController animated:YES];
}

- (void) loginWithLogin:(NSDictionary *)dict relogin:(BOOL)relogin
{
        NSString *_login = [dict valueForKey:@"email"];
        NSString *_pass = [dict valueForKey:@"pass"];
        NetworkOperation *op = [NetworkOperation operationLogin:_login pass:_pass];
        [NetworkOperation enqueueOperation:op observer:self];
        if (!relogin && !loginController)
                [self doLogin:dict];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        session = nil;
        loginController = nil;

        return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        NSString *login = [[NSUserDefaults standardUserDefaults] stringForKey:@"email"];
        NSString *pass = [[NSUserDefaults standardUserDefaults] stringForKey:@"pass"];
        NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];
        if ((login == nil || login.length == 0) ||
            (pass == nil || pass.length == 0) ||
            (name == nil || name.length == 0)) {
                if (!loginController) {
                        [self.window makeKeyAndVisible];
                        [self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.1];
                }
        } else {
                NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:login, @"email",
                                      pass, @"pass", name, @"name", nil];
                [self loginWithLogin:dict relogin:(session != nil)];
        }
}

So your app delegate, checks if you have your pass and login stored. If yes then it uses them to login and switches to your root view controller. In my case it is tabbed one to show couple tabs with balances, transactions, etc.

If there is no login and pass then show login view controller on top of your root view controller.

Here @session is your current session. Login is using enqueued operations as you see in the code.

If you need more details, just ping me.

Cynichniy Bandera
  • 5,991
  • 2
  • 29
  • 33
0

You could select your ViewController via storyboard-names:

 UIStoryboard *yourStoryboard = [UIStoryboard storyboardWithName:@"yourstoryboard"
                                                          bundle:nil];
 YourViewController *vc = [yourStoryboard instantiateViewControllerWithIdentifier:@"yourViewControllerIdentifier"];

and then present vc

Tobias
  • 1,220
  • 17
  • 35
0

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; if ([self isUserLoggedIn]) {
// Show the dashboard self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"first"]; } else { // Login self.window.rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"second"]; }