Best practice on showing a one time login screen w/ storyboards

前端 未结 3 1697
南旧
南旧 2021-02-14 23:36

I\'ve seen similar questions here, but not with a clear answer. So I have one modal login view with the classic username/password form, a Facebook login button and a Sign Up but

相关标签:
3条回答
  • 2021-02-15 00:02

    You can set the rootViewController through the AppDelegate by simply setting up a navigation controller, and when you do the check, set the navigation controllers root view to whichever view you want to be shown at that time. I think something like this should work if you add an if statement for what you want to do:

    // Override point for customization after application launch.
        RootViewController *rootController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
    
        self.window.rootViewController = navigationController;
    
    0 讨论(0)
  • 2021-02-15 00:03

    In my opinion the best strategy for something like this is a login screen that's already presented over the main view controller when the app launches, and is dismissed nicely and deallocated after the user signs in. I've found that most of the previously suggested solutions (as well as the suggestions here: Best practices for Storyboard login screen, handling clearing of data upon logout) do not accomplish this elegantly.

    After some experimenting yesterday, I think the best way of doing this is by using child view controllers:

    1. Choose your Main Interface storyboard in Xcode just as you normally would (there is no need to add anything to your appDelegate

    main interface

    2. Add the following to your main view controller in viewDidLoad:

    // If user is not logged in, show login view controller
    if (!isLoggedIn) {
        // Instantiate Login View Controller from storyboard
        UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        UIViewController *loginVC = [mainSB instantiateViewControllerWithIdentifier:@"Login"];
    
        // Set the Login View Controller's frame
        loginVC.view.frame = self.view.bounds;
    
        // Add login screen as a subview and as a child view controller
        [self.view addSubview:loginVC.view];
        [self addChildViewController:loginVC];
        [loginVC didMoveToParentViewController:self];
    
        // Maintain a reference to the Login screen so we can dismiss it later
        _loginVC = loginVC;
    }
    

    3. After the user has logged in, inform your main view controller by either using notifications or a delegate. Then you can animate the login screen away in any way you wish. Here I'm using a dissolve animation:

    // Animate out the category chooser
    [UIView animateWithDuration:0.2 animations:^{
        // Dissolve the login screen away
        [_loginVC.view setAlpha:0];
    } completion:^(BOOL finished) {
        // Remove login screen as a child view controller
        [_loginVC willMoveToParentViewController:nil];
        [_loginVC.view removeFromSuperview];
        [_loginVC removeFromParentViewController];
    
        // nil out property
        _loginVC = nil;
    }];
    

    And that's it! This way, the main view controller is always your window's root view controller, the login screen gets deallocated after the user logs in, and there is no flicker when first presenting the login screen.

    0 讨论(0)
  • 2021-02-15 00:13

    After trying many different methods, I was able to solve this problem with this:

    -(void)viewWillAppear:(BOOL)animated {
    
        // Check if user is already logged in
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        if ([[prefs objectForKey:@"log"] intValue] == 1) {
            self.view.hidden = YES;
        }
    }
    
    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
    
        // Check if user is already logged in
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        if ([[prefs objectForKey:@"log"] intValue] == 1) {
            [self performSegueWithIdentifier:@"homeSeg3" sender:self];
        }
    }
    
    -(void)viewDidUnload {
        self.view.hidden = NO;
    }
    
    0 讨论(0)
提交回复
热议问题