Reputation: 2006
I have a "Splash screen" ViewController extending UIViewController, set as the the initial app VC in my storyboard. This controller features a login form.
When the app starts, and before anything is displayed on the screen, I want this splash VC to check the user defaults to see if the user is already logged in. If so, I want the splash VC to redirect to the app's home VC, all before anything is displayed on the screen.
If the user is not logged in, I want the Splash VC to finish loading, displaying the login forms.
How would I go about implementing this? Would I place all of these checks in the init methods? I was having a hard time getting any code in the splash VC init methods to run at all, for some reason these methods don't get called.
Code in the viewDidLoad method runs fine, but running the code there would defeat the purpose of trying to allow the already-logged-in-user to start the app right into the home screen.
Suggestions? Thanks in advance.
Upvotes: 2
Views: 1668
Reputation: 4473
My pick of the place to put this logic is in application didFinishLaunchingWithOptions:
of application delegate. Here is how it would look :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//////////////////////////////////////////////
// 1. do loading data etc.
// 2. check whether user is signed in or not
//////////////////////////////////////////////
if(already signed in)
{
dispatch_sync(dispatch_get_main_queue(), ^{
[self.window.rootViewController performSegueWithIdentifier:@"segue identifier to home VC" sender:self.window.rootViewController];
});
}
else
{
dispatch_sync(dispatch_get_main_queue(), ^{
[self.window.rootViewController performSegueWithIdentifier:@"segue identifier to login VC" sender:self.window.rootViewController];
});
}
});
return YES;
}
And this is my quick storyboarding to assist the code. Hopefully you get the idea.
Upvotes: 4