12

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

aVC
  • 2,254
  • 2
  • 24
  • 46
  • I need some clarity , You have 1 tab bar with 2 icons & one for public and another one for non public which is protecting from authentication , right? – Kumar KL May 03 '13 at 04:01
  • @KumarKl No. Only One Tabbar which has two tabs now. The login screen just has a navigation bar to put the login button. – aVC May 03 '13 at 04:03
  • K I got it .. wait I will put some stuff – Kumar KL May 03 '13 at 04:04
  • @KumarKl Ok. To clarify, All tabs in the tabbar controller needs login to be accessed. Thats why I need to present the login screen in the beginning – aVC May 03 '13 at 04:13

2 Answers2

12

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];
        } 
Kumar KL
  • 15,315
  • 9
  • 38
  • 60
  • 1
    Feel free to ask , If you have any problem facing – Kumar KL May 03 '13 at 04:35
  • 1
    Thanks very much. By viewController ( you mentioned to add the code), did you mean the loginViewController? – aVC May 03 '13 at 04:36
  • 1
    Yes . You have LoginViewController or ViewController right . Login Page which has the main page right ? – Kumar KL May 03 '13 at 04:52
  • 1
    I have the login Page (LTController.m) and Then two views corresponding to two tab views – aVC May 03 '13 at 04:58
  • 1
    When you run your app . which is the initialise viewController -- LTControllr(Login Page ) right? – Kumar KL May 03 '13 at 05:08
  • 1
    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? – aVC May 03 '13 at 05:12
  • 1
    ok see here . You just change your initialise view in the storyboard to the Login View of NavigationController – Kumar KL May 03 '13 at 05:12
  • 1
    Yes .. in your storyboard above it has initialising from tabBar controller . Change that to LoginController – Kumar KL May 03 '13 at 05:13
  • 1
    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 May 03 '13 at 05:16
  • 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. – aVC May 03 '13 at 05:44
  • Something like this.. " UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *obj=[storyboard instantiateViewControllerWithIdentifier:@"tab2"]; [self.navigationController pushViewController:obj animated:YES]; " – Kumar KL May 03 '13 at 06:08
  • I tried it. Gave the view on tab2 an identifier "tab2", it pushes, But the tab bar is missing. – aVC May 03 '13 at 11:39
  • Actually it is missing both navigationBar and Tab Bar when I do this. Has it something to do with setting root VC? – aVC May 03 '13 at 12:32
  • Kumar, can you please check why the tab bar is not showing with this approach? would appreciate some inputs. – aVC May 06 '13 at 20:25
  • @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 . – Kumar KL May 07 '13 at 07:19
  • How do i skip the login screen if the user is already logged in previously? – Van Du Tran Nov 29 '13 at 20:09
  • You can assign a Bool Value. Or Store it to the NSuserDefaults. – Kumar KL Nov 30 '13 at 09:02
10

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.

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
CocoaEv
  • 2,984
  • 20
  • 21
  • 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. – aVC May 03 '13 at 04:38
  • 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 May 03 '13 at 05:18
  • 1
    If you present the login screen with a segue, then you can dismiss it with an unwind segue. See http://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-to-use-them – Sofi Software LLC Oct 30 '13 at 17:27
  • 1
    You should implement it in ViewDidAppear or else it may not work – Suraj K Thomas Jan 13 '14 at 10:09