luckyShubhra
luckyShubhra

Reputation: 2751

Logout from iOS App

I am a newbie in this field. I am working on an app in which user can logout from any page inside the app.

I'm using this method for my log out process. (referred from What is the perfect way to make a logout from IOS app?)

 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
      if (buttonIndex == 0) {
          NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
          [defaults setObject:nil forKey:@"UserId"];
          [defaults synchronize];
          //redirect to login view

          NewClassMoonAppDelegate * appsDelegate =[[UIApplication sharedApplication] delegate];
          LoginViewController *second = [[LoginViewController alloc]initWithNibName:nil bundle:nil];
          [appsDelegate.window setRootViewController:nil];
          [appsDelegate.window setRootViewController:login];
     }
}

My question is how to close all the open ViewController before performing logout? When I m implementing above method, page on which I had clicked logout button remains open in background. Can anyone help me regarding the same. Thanks in advance.

Upvotes: 0

Views: 208

Answers (2)

Cyrille
Cyrille

Reputation: 25144

That's a perfect job for a NSNotification: when the user is taps a Logout button, you fire a custom notification like so:

[[NSNotificationCenter defaultCenter] postNotificationName:@"userWillLogoutNotification" object:nil userInfo:nil];

Then every view/navigation/tabbar/whatever controller can react accordingly and "reset" itself.

It's not the job of the Logout button to perform this navigation task, every controller just handles its own business and reacts to application-wide notifications like this.

Upvotes: 1

MarkHim
MarkHim

Reputation: 5766

To dismiss all modal UIViewControllers first, you could do sth. like in this method

-(void)dismissModalStack {
    UIViewController *vc = self.presentingViewController;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:NULL];
}

(as seen here:iPhone - dismiss multiple ViewControllers)

instead of

[self.navigationController popToRootViewControllerAnimated:YES];

since this only pops your pushed UIViewControllersout of your navigation stack.

So for you it would be

-(void) actionSheet: (UIActionSheet * ) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex {
    if (buttonIndex == 0) {
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject: nil forKey: @"UserId"];
        [defaults synchronize];

        UIViewController *vc = self.presentingViewController;
        while (vc.presentingViewController) {
            vc = vc.presentingViewController;
        }
        [vc dismissViewControllerAnimated:YES completion:NULL];

        NewClassMoonAppDelegate * appsDelegate = [[UIApplication sharedApplication] delegate];
        LoginViewController * second = [[LoginViewController alloc] initWithNibName: nil bundle: nil];
        [appsDelegate.window setRootViewController: login];
    }
}

Upvotes: 1

Related Questions