Patrik Dendis
Patrik Dendis

Reputation: 313

Different UIViewController logged in

When user open the app it show login UIViewController. When user log in it redirect to next mapkit UIViewController. But when user open app again I would like that he will skip login UIViewController. How can I do that? I tried programatically redirect in login method viewWillAppear but it works bad(It show controller for second).

Map controller p

Upvotes: 0

Views: 38

Answers (3)

Glorfindel
Glorfindel

Reputation: 22651

Instead of checking the login in the viewWillAppear: method of the first view controller, do it in the didFinishLaunchingWithOptions: method of your application delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if (loggedIn) {
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
        [navigationController.topViewController performSegueWithIdentifier:@"1to2" sender:navigationController.topViewController];
        return YES;
    }
}

where 1to2 is the identifier of the segue from view controller 1 to 2.

Upvotes: 1

Mahmoud Adam
Mahmoud Adam

Reputation: 5852

A better way is to add this check in the AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if(isLoggedin) {
        Storyboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *mapViewController = [storyboard instantiateViewControllerWithIdentifier:@"mapViewController" ];
        self.rootViewController = [[UINavigationCotroller alloc] initWithRootViewController:mapViewController];
    }
    return YES;
}

Upvotes: 1

Nikita Took
Nikita Took

Reputation: 4012

You can do it like this:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // replace with your real auth-checking
    if ([self wasAuthorized] == NO) {

        [self showLoginController];

    }

    return YES;

}

- (UIViewController *)mainController {

    UINavigationController *rootNavigationController = (UINavigationController *)[[self window] rootViewController];
    return [[rootNavigationController viewControllers] firstObject];

}

- (void)showLoginController {

    UIViewController *loginController = [self loginController];
    dispatch_async(dispatch_get_main_queue(), ^{

        [self.mainController presentViewController:loginController animated:YES completion:nil];        

    });

}

- (UIViewController *)loginController {

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    return [mainStoryboard instantiateViewControllerWithIdentifier:@"LoginNavController"];

}

Upvotes: 0

Related Questions