Example for login screen modally based on storyboard

I am learning ios/ xcode and at a roadblock.

I have a tabbarcontroller+navigation based design. I need to present a login screen if user is not logged in. Here is the basic heirarchy. The login page needs a navigationBar (as the tutorial I followed puts a "Go" button on the bar.

LoginController: (LTController.h,.m)

Main View:TabBarController>
                   NavigationController>View1>View1a
                   NavigationController>View2

Storyboard layout

I read lot of posts here on modal view, delegate method, etc. Most of them are code snippets which unfortunately are a bit over the head for my beginner level.

Would appreciate a simple explanation as to how to achieve this. Espacially a note on which files needs changes would be great.

thanks


Solution 1:

Here is scenario . Its so simple . I just hope that it will be useful.

enter image description here

For the UITableBarController give a name for identity to storyboard identer image description here

Then in your ViewController class file You have the authentication credentials right >.? Do some stuff over there for authentication . then follow this code . It works fine

- (IBAction)Login:(id)sender {

    if(authenticated)  // authenticated---> BOOL Value assign True only if Login Success
        {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
            UITabBarController *obj=[storyboard instantiateViewControllerWithIdentifier:@"tab"];
            self.navigationController.navigationBarHidden=YES;
            [self.navigationController pushViewController:obj animated:YES];
        } 

Solution 2:

it looks like you are off to a good start. Since you have a tabbar design, you have to make a choice on how to present the login page and when you will do that.

you have to either present it before the tabbar is shown, or put logic in your first view controller to initiate the login process. There are other ways as well, but they get more complicated and I wanted to give you basic choices right now.

Here is the general concept I'd recommend.

a) create a persistent storage variable somewhere to determine if a user is logged in or not.

b) add a check for this flag in the View will load method of the first view controller attached to your tabbar.

c) present a modal login page directly from the view controller. if they login, great dismiss it, if not, they are stuck on the modal page.

so, here is basically how to do that:

for purposes of explaining, I'll going to call your first view controller - first tab on your tabbar controller - fviewController - ok?

in fviewController.m

-(void)viewDidLoad {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([[defaults objectForKey:@"loggedIn"]boolValue]) {
       NSLog(@"user is logged in - do nothing");
    }
   else {

       NSLog(@"User is not logged in");
       [self  performSegueWithIdentifier:@"LoginPage" sender:self];
   }

}

a couple more points it looks like you are using storyboards and segues. In that case, you would do the following:

  • create a new view controller for your login page
  • control drag a segue connection to it from the first view controller in your tabbar
  • identify the segue as "modal"
  • crate a new view controller class for the login view controller
  • present your view and manage your authentication
  • if the user is logged in, you need to store that back to the NSUserDefaults Note: if you have multiple users or other schemes, you may want to modify the single value I showed you in the example go track status for current user. Also: if you have logout code, you need to set the flag correctly. Also: if users are going to login and logout frequently, then use view will appear instead of view did load.

To flip the status:

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   [defaults setValue:[NSNumber numberWithBool:YES] forKey:@"loggedIn"]; //in
   [defaults setValue:[NSNumber numberWithBool:NO] forKey:@"loggedIn"]; //out

   do this in your login controller

To dismiss the modal view. Technically you should use a delegate callback to do this, but if you are trying to keep things simple, this should be ok

       [self dismissViewControllerAnimated:YES completion:^{

        }];

So your logic would be like this - did they login? Yes, then set YES status for logged in and then dismiss. If they dod not login, do nothing. They are stuck.

Finally, if you need to setup your login controller, you would use the method: prepareForSegue ... to initialize variables before the segue occurs. You probably have read about it if you are doing some tutorials.

Well ... hope that helps. It is a very basic approach. If you get that working, you can continue to add more security and capabilities to it as you go.

best of luck.