Basic user registration with Objective-C

After this tutorial, you will be able to build a user system like this one:




In order to continue with this tutorial, you should setup your project properly, as described in the Quickstart:


Once the project is setup and the Parse SDK is working properly, you should continue with the next step.

Step 1 – The login function

Creating a login function with back4app is amazingly simple. It is sufficient to use the following code:

[PFUser logInWithUsernameInBackground:username_here password:password_here
 block:^(PFUser *user, NSError *error) {
    if (user) {
        // Show some greeting message
    } else {
        // Something went wrong

This code should be added to the element of your app that does the login, as the values described in the code should be changed accordingly. The next step describes the register function.

Step 2 – The register function

Equally simple is the function to register the client, which is shown below:

PFUser* user = [PFUser user];
user.username = username_here;
user.password = password_here;
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    if(!error) {
        // No problems so far
    else {
        NSString* errorString = [error userInfo][@"error"];
        // Something went wrong; the error string is in errorString

Which should be put in whatever element triggers the registering mechanism. The next step integrates both functions to the app that is going to be built.

Step 3 – Integrating the code to the app

Start out by creating two labels, two buttons and two text fields, as well as setting up their style. As these elements have been set, we should append objects in the code to represent the text fields and add functions to be triggered as soon as the buttons are pressed. To do so, you should click on the element you want to add the event, then hold Control and drag the element to the code you want to add, as shown below for the login button:

Note: this has to be done in the ViewController.m, not the ViewController.h




As well as connect the text fields to the app, shown below for the login text field:

Note: this has to be done in the ViewController.h or the equivalent file




Now we add the code to each section of the buttons, noting that we will alert to the user if the login was successful or the registration was done. The resulting code is in the github repository: