iOS

User Registration

Introduction

Most real-world applications often utilize user-based features to provide a more personalized service to clients. These functionalities require the client to sign up on the app. With the Back4App platform and the ParseSwift SDK, you will be able to implement those features in your apps simply and quickly.

Prerequisites

To complete this tutorial, you will need:

Goal

To implement a user registration feature on an iOS App using the ParseSwift SDK.

Step 1 - Understanding the user registration flow

In order to integrate a signup option on an iOS app, it is necessary to create an object that conforms to the ParseUser protocol. This protocol implements the main required properties so that Back4App is able to store and manage login information. The following snippet shows how a user object can be implemented:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import Foundation
import ParseSwift

struct User: ParseUser {
  // Additional properties required by the ParseUser protocol
  var authData: [String : [String : String]?]?
  var originalData: Data?
  var objectId: String?
  var createdAt: Date?
  var updatedAt: Date?
  var ACL: ParseACL?
  
  // Main properties linked to the user's information
  var username: String?
  var email: String?
  var emailVerified: Bool?
  var password: String?

  // A custom property
  var age: Int?
}

As it can be seen in the above snippet, ParseSwift allows us to have a very flexible implementation for a user object. Similar to the age field, we can add as many additional fields as needed.

Once we have the User object ready, the ParseUser protocol gives this object a set of methods to handle all the necessary user operations such as sign up, log in, log out, etc.

In the following step, we first take a look at how to implement a signup request.

Step 2 - Creating a signup request

We start by adding the corresponding form where the user enters their account information. Let ViewController (a subclass of UIViewController) be the class where we implement the form. In the snippet below, we remark the key elements a basic signup form should have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import UIKit
import ParseSwift

class ViewController: UIViewController {
  // User inputs
  @IBOutlet weak var usernameTextField: UITextField!
  @IBOutlet weak var emailTextField: UITextField!
  @IBOutlet weak var passwordTextField: UITextField!
  
  // Sign up button
  @IBOutlet weak var signUpButton: UIButton!
    
  override func viewDidLoad() {
    super.viewDidLoad()
        
    // Add additional code if needed
  }

  // Called when the user taps on the signUpButton
  @IBAction func handleSignUp(_ sender: Any) {
    guard let username = usernameTextField.text, let password = passwordTextField.text else {
      return showMessage(title: "Error", message: "The credentials are not valid.")
    }
        
    signUp(username: username, email: emailTextField.text, password: password)
  }
    
  // This method prepares and registers the new user
  private func signUp(username: String, email: String?, password: String) {
    // TODO: Here we will implement the signup action
  }
    
  // Presents an alert with a title, a message and a back button
  func showMessage(title: String, message: String) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        
    alertController.addAction(UIAlertAction(title: "Back", style: .cancel))
        
    present(alertController, animated: true)
  }
}

We leave the layout part to the developer. You can integrate and set up the visual components according to your needs.

Next, in the following step, we implement the signUp(username:email:password:) method.

Step 3 - Implementing the signup function

The first step for signing up a user is to have an instance of a User object with its corresponding credentials. The username and the password fields are mandatory to register a new user, the remaining fields are optional. Therefore, a typical way to instantiate a User object would be:

1
2
var newUser = User(username: "aCoolUsername", email: "[email protected]", password: "mySecurePassword")
newUser.age = 25

Additionally, we should also provide the initial values for the custom fields, like the age field in our case.

The next step is to perform the signup action. The ParseUser protocol implements the method signup(...) that will allow us to send the signup request to the Back4App application. There are three types of implementations for signup(...). Depending on the use case, one should pick the appropriate one.
Now, we can complete the signUp(username:email:password:) in ViewController:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class ViewController: UIViewController {
  ...

  private func signUp(username: String, email: String?, password: String) {
    var newUser = User(username: username, email: email, password: password)
    newUser.age = 25 // WARNING: This should be entered by the user, not hardcoded

   //WARNING: ONLY USE ONE OF THE FOLLOWING CASES, THE SYNCHRONOUS OR THE ASYNCHRONOUS CASE  

    // The below registers the user synchronously and returns the updated User object (stored on your Back4App application)
    do {
      let signedUpUser = try newUser.signup()
      showMessage(title: "Signup succeeded", message: "\(user)")
      usernameTextField.text = nil
      emailTextField.text = nil
      passwordTextField.text = nil
    } catch let error as ParseError {
      showMessage(title: "Error", message: error.message)
    } catch {
      showMessage(title: "Error", message: error.localizedDescription)
    }

    // The below registers the user asynchronously and returns the updated User object (stored on your Back4App application) wrapped in a Result<User, ParseError> object
    newUser.signup { [weak self] result in
      switch result {
        case .success(let signedUpUser):
          self?.showMessage(title: "Signup succeeded", message: "\(signedUpUser)")
          self?.usernameTextField.text = nil
          self?.emailTextField.text = nil
          self?.passwordTextField.text = nil
        case .failure(let error):
          self?.showMessage(title: "Error", message: error.message)
      }
    }
  }
}

Note: Registering a new user using the signup(...) method automatically logs in the user, so there is no need for the user to log in again to continue using your app.

At any time during your app’s lifecycle, you can have access to the currently logged-in user from a static property implemented in the ParseUser protocol

1
let loggedInUser: User? = User.current

In this repository you can find a simple user registration app that follows the steps we detailed above.

Conclusion

The Back4App platform together with the ParseSwift SDK offers a quick and staightforward way to integrate a signup flow onto your iOS apps. Furthermore, in the following guides, we will explore the remaining procedures like log in, log out, etc.