iOS
...
Parse Swift SDK
Users

User Log In

10min

User log in and log out

Introduction

In the user registration guide we learned how to integrate a signup option into an iOS app using the Back4App platform and the ParseSwift SDK. Once a user successfully signs up in your app, logging in and logging out actions are key features within the app’s flow.

The ParseSwift SDK will allow us to integrate these features seamlessly into any iOS app.

Prerequisites

To complete this quickstart, you need:

Goal

To implement a user login and logout feature using the ParseSwift SDK and the Back4App platform.

1 - Setting up the login and logout features

Before starting to implement any login functionality, we have to create the object that will represent the user. For simplicity, we will reuse the same User struct (which conforms to the ParseUser protocol) we introduced in the user registration guide:

Swift


We recommend following the user registration guide and registering at least one user to use it as an example for this guide.

Similar to the signup process, logging in requires a form where the user enters their username and password. Then, we perform a login request using the corresponding methods provided by the ParseSwift SDK. In its turn, Back4App processes the request and returns a response containing the login information. When an error occurs, the response returns information to identify and handle this error.

The logout process is straightforward. The ParseSwift SDK allows us to implement it in a single line of code.

2 - Setting up the app

Once you connected your Xcode project to your Back4App application, the next step is to set up the app’s user interface.

For the login process, we will implement a simple controller containing the corresponding input fields and a login button:

Document image


The class in charge of this form is called LogInController and it is a subclass of UIViewController. The key components to integrate into this controller are two UITextField’s and one UIButton. The following snippet shows the implementation of the LogInController class:

Swift


Additionally, the helper function showMessage(title:message:) is implemented in an extension of UIViewController:

Swift


For the logout process, we insert a button in the home controller, i.e., HomeController. This view controller will only contain the logout button and a label showing the user’s username:

Document image


The implementation of this view controller is straightforward:

Swift


3 - Login request

We now proceed to implement the logIn(with:password) method in the LogInController class. The ParseUser protocol gives the User object the static method login(username:password). This method prepares and sends the login request to your Back4App application. Depending on the use case, one can use one of the many implementations of the login(...) method. We now complete the logIn(with:password) method in LogInController:

Swift


4 - Logout request

The logout request is as simple as the login request. Once again, the ParseUser protocol provides the User with the static method logout(...). By calling this method the current user (accessed via User.current) logs out from your Back4App application. We will call this method when the user taps the logout button located on the home screen, i.e., in the handleLogOut() method in the HomeController class, we add the following:

Swift


5 - Run the app!

In this repository, you will find an Xcode project containing the login and logout processes we described above. Before running the app, make sure you connected the Xcode project to your Back4App application.

Conclusion

The Back4App and the ParseSwift SDK allow us to integrate login and logout features in iOS apps in a quick way. After connecting your Back4App application with your Xcode project, the login (or logout) process only requires calling a single method.