Login Screen with Storyboarding possible?

14,221

Solution 1

I have solved it by putting a login view without any segues (to or from it) like in the screenshot below:

enter image description here

Then, I used a custom class in the tab bar controller to show it whenever I need it.

In the tab bar controller class, I use 'viewDidLoad' to fire up the login view. To show the modal view, I do have a singleton thingy that stores some state, say BOOL isAuthenticated, where I do the magic:

- (void) performLoginIfRequired: (UIViewController *) source {

   if (!self.isAuthenticated) {

       NSLog(@"Is not authed");

       UIStoryboard *storyboard = [UIApplication sharedApplication].delegate.window.rootViewController.storyboard;

       UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];

       [source presentModalViewController:loginController animated:YES];

   } else {
       NSLog(@"Is authe");

   }
}

And, in my case, I wanted it to be shown when the app first starts, but also when it enters foreground again. So, I registered my tab bar controller with the notification center, so I get notified if the app is coming back:

-(void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

In the willEnterForeground method, I do:

-(void) willEnterForeground: (NSNotification *)notification {
    [[myStateThingy defaultState] performLoginIfRequired:self]; 
}

Solution 2

It sounds like you need to use the performSegueWithIdentifier method. Make sure both views are in the same storyboard, link them together using a Push segue, and give that segue a name. Then, from your first view controller's code simply call the performSegueWithIdentifier to perform a manual segue.

Hope this helps!

See also: Conditionally following a segue

Cheers, Jesse L. Zamora

Solution 3

I had this same issue, and I solved it simply by doing the following: Instead of trying to segue to a login screen(modally or push), I made the login screen my root view controller. In the login view controller's viewWillAppear method, I check if someone's logged in already. If so, I push my home screen:

// mutableFetchResults is an array with my persistent Credentials object
if ([mutableFetchResults count] > 0) { // Someone's already logged in
  [self performSegueWithIdentifier:@"Home" sender:self];
}

Also, in the Home screen view controller's viewWillAppear method, I hid the back button with this line, so the user can't go "back" to the login screen:

self.navigationItem.hidesBackButton = YES;

Finally, every page of my app has a "Sign Out" bar button on the top right. Signing out and putting the login screen up was as simple as this:

- (IBAction)signOutButtonPressed:(UIBarButtonItem *)sender {
  MyAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  [appDelegate signOutCurrentUser]; // this method in my app delegate deletes the current Credentials
  [self.navigationController popToRootViewControllerAnimated:YES];
}

Hope that was simple enough!

Share:
14,221

Related videos on Youtube

eemceebee
Author by

eemceebee

Updated on July 02, 2022

Comments

  • eemceebee
    eemceebee almost 2 years

    I am playing around with the new iOS 5 features and trying to rewriting one of my apps as pure iOS 5 app using the new storyboarding feature.

    To cut a long story short, I have a start screen where the app tries to connect to a server if the user saved some login data, if not, it should ask for them.

    Here is how I would do it. I create a Viewcontroller which is doing the connection thing in the viewDidLoad method. If there is no login data or the login is not successful, I need a to do a manual segue to the login screen.

    Now is this even possible, or do I need 2 story boards for that ?

  • Sam Grossberg
    Sam Grossberg about 12 years
    What is "source" in this example?
  • Daniel Albert
    Daniel Albert about 12 years
    I have the same question, what is source in this example? Maybe the current shown View Controller?
  • Papick G. Taboada
    Papick G. Taboada almost 12 years
    I made changes to my answer to clarify "source", which in my case, is the UiTabController. Sorry for the late answer.
  • norman784
    norman784 over 11 years
    also you can put your performLoginIfRequired in the method viewDidAppear
  • expert
    expert almost 11 years
    Hod do you dismiss your modal login view ? I get following error: Warning: Attempt to dismiss from view controller <MyTabBarController: 0xc5735c0> while a presentation or dismiss is in progress!
  • Abdul Yasin
    Abdul Yasin almost 10 years
    Nice solution.Plus one. I hope there is another solutions as well.