iOS

Sign-up/Sign-in - ObjC

17min

Login and User registration tutorial using XCode and Back4App

Introduction

This section explains how you can create an app with a simple user registration using Parse Server core features through Back4App.

This is how it will look like:

Document image


At any time, you can access the complete Project built with this tutorial at our GitHub repository.

To complete this quickstart, you need:

1 - Set up

  1. Add another view controller called LoggedInViewController. In the main storyboard drag a view controller on to the canvas and set the class to LoggedInViewController and set the Storyboard ID to LoggedInViewController
  2. In both ViewController.m and LoggedInViewController.m make sure to include the Parse module by including it at the top of the file.
#import <Parse/Parse.h>

2 - Create your Sign Up and Login UI

Logging in creates a Session object, which points to the User logged in. If login is successful, ParseUser.CurrentUser() returns a User object, and a Session object is created in the Dashboard. Otherwise, if the target username does not exist, or the password is wrong, it returns null.

The method used to perform the login action is ParseUser.logInWithUsername(), which requires as many arguments as the strings of username and password, and may call a callback function.

Note: After signing up, login is performed automatically.



  1. Drag four UITextFields onto the ViewController in the main storyboard. Center the textfield and put two at the top and two at the bottom of the view controller. Drag two more UIButtons on to the view and place them below the textfields. Set the top button text to say Sign In. Set the bottom bottom to say Sign Up. Set the text fields to say username and password. It should look like this.
Document image

  1. 2. Next we are going to connect your UITextFields in your storyboard to properties in your view controller. Add the following properties to the top of ViewController.m. Next go to your storyboard and right click on each UITextField and click on the reference outlet then drag a line back to the ViewController icon and set it to the appropriate field. signInUsernameField connects to the sign In Username Field, etc… Finally we will add a UIActivityIndicatorView for later.
@interface ViewController (){ IBOutlet UITextField * signInUserNameTextField; IBOutlet UITextField * signInPasswordTextField; IBOutlet UITextField * signUpUserNameTextField; IBOutlet UITextField * signUpPasswordTextField; UIActivityIndicatorView *activityIndicatorView; }

2. Then in the viewDidLoad method set the UIActivityIndicatorView to be attached to the middle of the screen.

- (void)viewDidLoad { [super viewDidLoad]; CGRect frame = CGRectMake (120.0, 185.0, 80, 80); activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:frame]; activityIndicatorView.color = [UIColor blueColor]; [self.view addSubview:activityIndicatorView]; }

3. Then in the viewDidAppear method check to see if you are already logged in. If you are logged in you will redirect the user to the LoggedInViewController.

- (void)viewDidAppear:(BOOL)animated { if ([PFUser currentUser]) { [self goToMainPage]; } }

4. Next lets add the goToMainPage method. It will redirec the user to the LoggedInViewController. Make sure the that the LoggedInViewController in the storyboard has its class and Storyboard ID set to LoggedInViewController.

- (void)goToMainPage { NSString * storyboardName = @"Main"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; LoggedInViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"LoggedInViewController"]; [self presentViewController:vc animated:YES completion:nil]; }

5. Now lets set up the IBAction that will connect to the SignUp button on the ViewController in the main storyboard.

- (IBAction)signUp:(id)sender { PFUser *user = [PFUser user]; user.username = signUpUserNameTextField.text; user.password = signUpPasswordTextField.text; [activityIndicatorView startAnimating]; [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { [self->activityIndicatorView stopAnimating]; if (!error) { [self goToMainPage]; } else { [self displayMessageToUser:error.localizedDescription]; } }]; }

6. We need to add the displayErrorMessage function to show any error messages from the server. We will use this method anytime we communicate with our parse app.

- (void)displayMessageToUser:(NSString*)message { UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Message" message:message preferredStyle:UIAlertControllerStyleAlert]; UIPopoverPresentationController *popPresenter = [alert popoverPresentationController]; popPresenter.sourceView = self.view; UIAlertAction *Okbutton = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]; [alert addAction:Okbutton]; popPresenter.sourceRect = self.view.frame; alert.modalPresentationStyle = UIModalPresentationPopover; [self presentViewController:alert animated:YES completion:nil]; }

7. Now that we can handle network activity and network errors lets set up the IBAction that will connect to the SignIn button on the ViewController in the main storyboard.

- (IBAction)signUp:(id)sender { PFUser *user = [PFUser user]; user.username = signUpUserNameTextField.text; user.password = signUpPasswordTextField.text; [activityIndicatorView startAnimating]; [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { [self->activityIndicatorView stopAnimating]; if (!error) { [self goToMainPage]; } else { [self displayMessageToUser:error.localizedDescription]; } }]; }

3 - Log Out

Logging out deletes the active Session object for the logged User. The method used to perform log out is ParseUser.logOutInBackgroundWithBlock().

  1. Drag a UIButton on to LoggedInViewController in the main storyboard. Set the button title to ‘Logout’. It should look like this.
Document image

  1. Lets add the displayErrorMessage function again to show any error messages from the server. We will use this method anytime we communicate with our parse app.
- (void)displayMessageToUser:(NSString*)message { UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Message" message:message preferredStyle:UIAlertControllerStyleAlert]; UIPopoverPresentationController *popPresenter = [alert popoverPresentationController]; popPresenter.sourceView = self.view; UIAlertAction *Okbutton = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]; [alert addAction:Okbutton]; popPresenter.sourceRect = self.view.frame; alert.modalPresentationStyle = UIModalPresentationPopover; [self presentViewController:alert animated:YES completion:nil]; }

2. Lets add the goToStartPage function to take us back to the login/signup screen after we logout.

- (void)goToStartPage { NSString * storyboardName = @"Main"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; ViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"]; [self presentViewController:vc animated:YES completion:nil]; }

3. Finally lets add the IBAction to execute the logout call and take us back to ViewController.m signup/login page. This method logs out the PFUser and takes you back to the signup page. Connect this IBAction to the logout button on LoggedInViewController.

- (IBAction)logout:(id)sender { [activityIndicatorView startAnimating]; [PFUser logOutInBackgroundWithBlock:^(NSError * _Nullable error) { [self->activityIndicatorView stopAnimating]; if(error == nil){ [self goToStartPage]; }else{ [self displayMessageToUser:error.debugDescription]; } }]; }

4 - Test your app

  1. Run your app and create a couple of users, also try logging in again after registering them.
  2. Login at Back4App Website
  3. Find your app and click on Dashboard>Core>Browser>User.
  4. Try logging in and out with the same user and signing back in.

At this point, you should see your users as displayed below:

Document image


Note: Using the codes displayed above, every time you log in with a user, a Session is opened in your Dashboard, but when the user logs out that particular Session ends. Also, whenever an unsuccessful login or sign up attempt occurs, the Session opened in Parse Server Dashboard is deleted.

It’s done!

At this stage, you can log in, register or log out of your app using Parse Server core features through Back4App!