Every time a user open’s your app you might have to check if they are logged in or not. Typically this is done in the View Controller that the user uses to log in. If the user is logged in, then they will be taken to the main screen, and if not, they are left in the login screen. However there is a problem with this approach since while checking if the user is logged in or not, the login view controller will still be presented briefly because the login screen is the initial screen to the app. This tutorial will present a simple solution that makes the app open smoothly!
Disrupted Login Flow
This is an example of what the app launch will look like with a sign in authentication and the login screen controller being the initial view controller.
The app goes from:
Launch screen -> Login Screen -> Main Screen
The app in opened which presents the launch screen, then the app checks to see whether the user is logged in or not. However during this process the login screen is displayed since it is the initial view controller. This is confusing for the user if they are already logged in and there is a better way to go about this!
Smooth Login Flow
When the app starts, we would like it to display the login in screen if the user is not logged in and the main screen if the user is logged in. This is done by introducing another view controller that I will call the welcome view controller, this will run the code that will check to see if the user is logged in or not. This view controller will also look exactly the same as the launch screen so the the user just sees your app starting up meanwhile it is checking if they are logged in or not.
The resulting storyboard will look like this.
The result for the case when the user is logged in will look like the following.
Launch Screen -> Welcome View Controller (Looks the same as the Launch Screen) -> Login Screen or Main Screen
The user will spend a little more time waiting for the app to launch, but this is a much smoother transition than visiting the log in screen briefly before quickly being redirected to the main screen of the app!