In this series of blog posts we aim to show you how to build a Simple Login Form in Swift. Back in Part 1 we left off with a Login Screen which opens when you run the app. In this part we are going to add a LoginService which will manage user authentication. If the user is already authenticated they will be directed to a Welcome Screen. Users who are not authenticated yet will see a Login Screen.
Let’s start by adding a Welcome View to our Main.storyboard. Drag a View Controller onto the Storyboard and within the Identity Inspector (⌘ + ⌥ + 3) enter the Storyboard ID “Welcome”. Drag a Button onto the View and position within the Size Inspector (⌘ + ⌥ + 5) as follows:
Double Click on the Button and change the button title to Sign Out. Switch to the Attributes Inspector (⌘ + ⌥ + 4) and set the Background colour as follows:
Set the Text Color to White and set the Font to System Bold.
We are going to manage user authentication from a Singleton class LoginService. I have described the Singleton Pattern in Swift in the following blog post Rocking the Singleton Pattern in Swift.
Add a New File ⌘N and choose Swift File, click Next and name the file LoginService. Open the LoginService and replace the contents with the following:
We’ve added quite a lot of code but it is all simple. We create a struct OAuthInfo to hold the users OAuth information. The struct manages the storage of OAuth data to NSUserDefaults. A function signOut clears out the stored NSUserDefaults.
The function loginWithCompletionHandler calls the function exchangeTokenForUserAccessTokenWithCompletionHandler which calls the Apigee Endpoint which is responsible for issuing OAuth tokens.
Add a New File ⌘N and choose Swift File, click Next and name the file ConnectionSettings. Open the ConnectionSettings file and replace the contents with the following:
Hooking Up The Login View Controller
Open up the Main.storyboard and click on the Login View Controller. Within the Identity Inspector (⌘ + ⌥ + 3) set the Class to LoginViewController. Open the LoginViewController and add the following method:
In the Main.storyboard you need to hook up the login function. Click on the Login View Scene Ctrl drag from Login to Login View and select the login function.
Hooking Up The Welcome View Controller
Open up the Main.storyboard and click on the Welcome View Controller. Within the Identity Inspector (⌘ + ⌥ + 3) set the Class to WelcomeViewController.
Add a New File ⌘N and choose Swift File, click Next and name the file WelcomeViewController. Open theWelcomeViewController file and replace the contents with the following:
The signOut function uses the LoginService to check whether the user is logged in or not. If logged in the controllerId is set to “Welcome”, “Login” if not. presentViewController is called to load the appropriate view controller, because we call signOut in the first line of this function it will always result in us showing the Login Screen.
The App Delegate
We now can hook up the App Delegate. Open the AppDelegate file and replace application(_:didFinishLaunchingWithOptions) with the following:
In the Main.storyboard you need to hook up the signOut function. Click on the SignOut View Scene Ctrl drag from Sign Out to View Controller and select the signOut function.
That’s A Wrap
Everything you need should be working now and running the App should result in the Login Screen being displayed. If you enter any username and password, tap Login you will see the Welcome Screen. Subsequent runs of the App will result in you seeing the Login Screen until you tap Sign Out. The completed code for this post is available here firstname.lastname@example.org:developerdave/SwiftLoginSample.git