Basic User Registration with Email Verification – Objective-C

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

 

app_working_email

 

Before going into the action, make sure you have a working project and know how to use the login and register functionalities of the back4app’s Parse API. You can check out the links below to the Quickstart and the Basic User Registration:

Quickstart

Basic user registration with Objective-C

Step 1 – Enable Email verification

To enable the Email verification, just go to your Back4app App page and find this option:

 

Screen Shot 2016-08-25 at 3.16.31 PM

 

Click to see more options and tick a checkbox just like this one:

 

Screen Shot 2016-08-25 at 3.16.37 PM

 

You are all set with the email verification on the server! Now, to the code.

Step 2 – Implementing the register function

To illustrate the functionality, we are going to implement a similar version of the Basic User Registration app. The key difference here is that the username is the user’s email, which simplifies a lot the login-registration mechanics. Hence, our register function will be as follows:

Note: Key changes have been highlighted

//Gather the relevant info
NSString* email = email_here;
NSString* password = password_here;
    
//We are not checking the email here; just for simplicity sake
PFUser* user = [PFUser user];
//This is the key change; username being equal to the email
user.username = email;
user.password = password;
//This is an additional and necessary info
user.email = email;

[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    if(!error) {
        // In case everything went fine
    }
    else {
        NSString* errorString = [error userInfo][@"error"];
        // In case something went wrong
    }
}];

We don’t have to check the email address format. Back4App does that for you. Now to the login part.

Step 3 – Implementing the login function

The additional step that has to be implemented in the login function is to check if the email has been verified. This has to be added on the success section of the code, shown below:

Note: Key changes have been highlighted

//This gathers the relevant info
NSString* email = email_here;
NSString* password = password_here;
    
[PFUser logInWithUsernameInBackground:email password:password
  block:^(PFUser *user, NSError *error) {
    if(user) {
        // Checking out if the email has been verified
        if([[user objectForKey:@"emailVerified"] boolValue]) {
            // Email has been verified
        }
        else {
            // Email has not been verified, logout the user
            [PFUser logOut];
        }        
    } else {
        NSString* errorString = [error userInfo][@"error"];
        // An error occurred
    }
}];

The full source code to the project is in the link below:

http://github.com/rvitorper/back4app-email-objc