6

In my app I would like to show a login screen - which will be displayed when the app starts and when the app becomes active. For reference, I am using storyboards, ARC and it is a tabbed bar application.

I therefore need to do the process in the applicationDidBecomeActive method:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if ( ... ) { // if the user needs to login
        PasswordViewController *passwordView = [[PasswordViewController alloc] init];
        UIViewController *myView = self.window.rootViewController;
        [myView presentModalViewController:passwordView animated:NO];
    }
}

To an extent this does work - I can call a method in viewDidAppear which shows an alert view to allow the user to log in. However, this is undesirable and I would like to have a login text box and other ui elements. If I do not call my login method, nothing happens and the screen stays black, even though I have put a label and other elements on the view.

Does anyone know a way to resolve this? My passcode view is embedded in a Navigation Controller, but is detached from the main storyboard.

2
  • @R.A Please see updated answer below. I am conducting final testing now, but it seems to work great!
    – Patrick
    Commented Nov 26, 2012 at 14:13
  • @Patrick Does this still work? What method are you using now?
    – SAHM
    Commented Jun 3, 2015 at 5:11

2 Answers 2

7

A variety of answers finally led me to an answer which doesn't seem too complicated so I will post it here - and it actually looks really good if I am honest.

Firstly, my password view is embedded in a Navigation Controller (Editor -> Embed In) and this is connected to the main tab bar controller using a modal segue with an id, in my case 'loginModal'.

In the applicationDidBecomeActive method put something like this:

[self performSelector:@selector(requestPasscode) withObject:nil afterDelay:0.2f];

And then put this function somewhere in the App Delegate

-(void)requestPasscode{
    if ( /* If the user needs to login */ ) {
        [self.window.rootViewController performSegueWithIdentifier:@"loginModal" sender:self];
    }
}

This will present your login view whenever the app begins or enters the foreground (for example, when switching apps).

NOTE: The above line will not work if the root of your app is embedded in a navigation controller.

There are however two bugs;

  1. If the user was previously viewing a modal view when they dismissed the app
  2. If the user dismissed the app on the password view.

Both of these cause the app to crash so the following line goes in the applicationWillResignActive method.

[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];

It basically dismisses all modal views that are presented. This may not be ideal, but modal views are more often then not, used for data entry and so in many cases, this is a desired effect.

-1

You should init PasswordViewController viewcontroller from xib or if you store UI in Storyboard you should use Segue for present this controller.

I can't say about another parts but that part seems to me very weird.

My passcode view is embedded in a Navigation Controller, but is detached from the main storyboard.

in storyboards you can store view controllers and view inside of view controllers so it's not good to store some view outside of viewcontroller because you will not be able to load this view from storyboard after receiving memory warning. Please correct me if I didn't get what do you mean.

If we are going by your way there is no difference load PasswordViewController at applicationDidBecomeActive or at your first view controller at Storyboards because you calling present view controller from first loaded view controller. So you can do it in your first view controller. Also you can store some hidden view inside of your first viewcontroller and show this view if the user needs to login.

I tested it. So at first your controller become loaded and then you got method applicationDidBecomeActive. So it's better to put your code inside -(void)viewDidAppear:animated method of your first viewcontroller.

Best regards, Danil

6
  • 1
    This is wrong. You can present a view controller without having to assign it to some strong property. UIKit will strongly retain it when it is presented. Commented Nov 22, 2012 at 16:31
  • in any case you should init this viewcontroller from xib or if you store UI in Storyboard you should use Segue for present this controller
    – Danil
    Commented Nov 22, 2012 at 16:38
  • The main problem is that my app is huge and it would be bad to have code in each view. But I don't think you can call a segue from the App Delegate
    – Patrick
    Commented Nov 22, 2012 at 16:40
  • Aaargh, I worked it out. I had tried to do a segue but never combined it with my above approach. This line seems to work: [myView performSegueWithIdentifier:@"loginModal" sender:self];
    – Patrick
    Commented Nov 22, 2012 at 16:46
  • Ok here is really interesting problem. If we are going by your way there is no difference load PasswordViewController at applicationDidBecomeActive or at your first view controller at Storyboards because you calling present view controller from first loaded view controller. So you can do it in your first view controller. Also you can store some hidden view inside of your first viewcontroller and show this view if the user needs to login
    – Danil
    Commented Nov 22, 2012 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.