Example for login screen modally based on storyboard

21,921

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.

Share:
21,921
aVC
Author by

aVC

Updated on July 05, 2022

Comments

  • aVC
    aVC almost 2 years

    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

  • Kumar KL
    Kumar KL about 11 years
    Feel free to ask , If you have any problem facing
  • aVC
    aVC about 11 years
    Thanks very much. By viewController ( you mentioned to add the code), did you mean the loginViewController?
  • aVC
    aVC about 11 years
    Thank you for the details. Yes I am going through a lot of tutorials. Unfortunately, I am starting from scratch including Obj C, so battling my odds. I really appreciate it. I have covered the login, segue and stuff. The modal view display was giving me nightmare. I will try this out.
  • Kumar KL
    Kumar KL about 11 years
    Yes . You have LoginViewController or ViewController right . Login Page which has the main page right ?
  • aVC
    aVC about 11 years
    I have the login Page (LTController.m) and Then two views corresponding to two tab views
  • Kumar KL
    Kumar KL about 11 years
    When you run your app . which is the initialise viewController -- LTControllr(Login Page ) right?
  • aVC
    aVC about 11 years
    Yes. login page (with the navigation controller) is the first view.I think now I understand. So in the login page which is the first view, Check for login, if(authenticated){ Do as you said;} correct?
  • Kumar KL
    Kumar KL about 11 years
    ok see here . You just change your initialise view in the storyboard to the Login View of NavigationController
  • Kumar KL
    Kumar KL about 11 years
    Yes .. in your storyboard above it has initialising from tabBar controller . Change that to LoginController
  • aVC
    aVC about 11 years
    Awesome... got it working. Thanks very much, Kumar. I will work on the minor tweaks, and hope its ok to throw in a question if any. :)
  • aVC
    aVC about 11 years
    I wish I could accept both answers. I am upvoting yours. I am onto NSUserdefaults tutorial now. Thanks for mentioning that. Thanks again for such detail and patience.
  • aVC
    aVC about 11 years
    If I need to go directly to a specific tab (lets say I give an identifier to that view), can I use the same method? I tried it but the tab bar does nt show.
  • Kumar KL
    Kumar KL about 11 years
    Something like this.. " UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *obj=[storyboard instantiateViewControllerWithIdentifier:@"tab2"]; [self.navigationController pushViewController:obj animated:YES]; "
  • aVC
    aVC about 11 years
    I tried it. Gave the view on tab2 an identifier "tab2", it pushes, But the tab bar is missing.
  • aVC
    aVC about 11 years
    Actually it is missing both navigationBar and Tab Bar when I do this. Has it something to do with setting root VC?
  • aVC
    aVC about 11 years
    Kumar, can you please check why the tab bar is not showing with this approach? would appreciate some inputs.
  • Kumar KL
    Kumar KL about 11 years
    @aVC : Sorry for Delay , For navigation Bar . You just put this code in "" self.navigationController.navigationBarHidden=NO; "" . I mean in the above code change to this .
  • Sofi Software LLC
    Sofi Software LLC over 10 years
    If you present the login screen with a segue, then you can dismiss it with an unwind segue. See stackoverflow.com/questions/12561735/…
  • Van Du Tran
    Van Du Tran over 10 years
    How do i skip the login screen if the user is already logged in previously?
  • Kumar KL
    Kumar KL over 10 years
    You can assign a Bool Value. Or Store it to the NSuserDefaults.
  • Suraj K Thomas
    Suraj K Thomas over 10 years
    You should implement it in ViewDidAppear or else it may not work