Pergunta

I currently develop an iOS app which allow users to consult their account (account create on a website).

I want that user can consult his account after connection. So, i want a first view which will be a login view. After accept connection, the login view disappear and the first view controller load data.

How can i do that?

Foi útil?

Solução

On viewDidAppear of FirstViewController add the following code to show Login ViewController before tab.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
    if (!delegate.login) {  // BOOL value to know if user is logged in or not.If user succefully logged in set value of this as YES else NO.
    LoginViewController * lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];

    [self presentModalViewController:lvc animated:NO];
    [lvc release];


    }
}

Outras dicas

you can use presentModalViewController(_:animated:completion:) to present the login view controller.

After successfull login use dismissModalViewControllerAnimated(_:completion:) to dismiss that view controller, so you can see your tab bar.

I tried the modal view controller approach (i.e., present your login view controller modally on startup, with animated: set to false), but there are issues: If you try to present it too early (i.e., viewWillAppear(animated:)), it doesn't show. If you try too late (i.e., viewDidAppear(animated:)), the underlying view controller "shows through" just for a split second (at least on iOS 8).

Instead, what I do is launch my app with the login view controller as the root view controller of the window (set as initial view controller of the main storyboard) and, once authentication succeeds, I "swap" the root view controller for my app's main tab bar controller (which I instantiate lazily form a separate storyboard).

To achieve this with a smooth transition (animation), instead of immediately, I use code like the following within the login view controller:

// Run this code when authentication succeeds:

UIView.transitionWithView(self.view.window,
            duration: 0.3,
            options: UIViewAnimationOptions.TransitionCrossDissolve,
            animations: {
                window.rootViewController = tabBarController
            },
            completion: nil
        )

(idea taken from here)

One issue I came across, though, was that the tab bar I was inserting contained a navigation controller in the selected (current) tab, and during the transition, the navigation bar would appear shifted upwards and "underlap" the app's status bar, only to jump back into its correct position upon animation completion. Very annoying... I fixed that by adding this line of code to the child view controller contained in the navigation controller:

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)

    self.navigationController?.navigationBar.layer.removeAllAnimations()
}

(fix taken from here)

UPDATE: I have notice that on some simulators (iPhone 6+, iOS 9.0), the navigation bar 'jump' does not occur, and instead a quick (but gentle) animation takes it into place.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top