Xcode: Display Login View in applicationDidBecomeActive

前端 未结 2 730
我寻月下人不归
我寻月下人不归 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.

    0 讨论(0)
  • 2020-12-17 01:54

    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

    0 讨论(0)
提交回复
热议问题