Xcode: Display Login View in applicationDidBecomeActive

前端 未结 2 737
我寻月下人不归
我寻月下人不归 2020-12-17 00:57

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 i

2条回答
  •  被撕碎了的回忆
    2020-12-17 01:39

    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.

提交回复
热议问题