Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

parse com logo

So, you want to be able to let people sign up to your app right? and you’re using Parse.com? Awesome. They have thought of this and created a fantastic specialised user class: PFUser. This tutorial will show you have to Create a user sign up based app with Parse.com using PFUser.

This tutorial assumes that you have at least a basic understanding of the Swift programming language, the structure of Xcode projects and finally how to create a new single view application in Xcode 6. If you need to be refreshed on any of these head over to our Swift Tutorials page for further reading.

It also assumes that you know how to add the Parse.com iOS SDK to your swift project, creating the bridging header and adding the necessary frameworks. If you do not know this, please check out this tutorial:

You will not need to follow that whole tutorial through to add users to your swift application using PFUser and I have added in a link back to this tutorial in the correct position when you should exit.

The user interface

The first thing we need to do to allow the ability for people to sign up to our app is to give them the fields to do so. In your Main.storyboard drag in these four elements and name them accordingly:

Element Name
UILabel messageLabel
UITextField usernameTextField
UITextField passwordTextField
UIButton loginVerifyButton

Double click the label and type in what ever message you would like, I have gone for: Please Sign Up. This could even be left blank if you wish. Then In the Attributes Inspector for the first UITextField type in the placeholder field Username. Then do the same for the second UITextField but using password and use email for the third. Next double click the UIButton and change the name to Signup. Arrange and resize accordingly and you should have something like this:

swift sign up with parse elements

Now, In your ViewController.swift file, just under class ViewController: UIViewController { add in this code:


@IBOutlet var usernameTextField: UITextField!
@IBOutlet var passwordTextField: UITextField!
@IBOutlet var emailTextField: UITextField!
@IBOutlet var messageLabel: UILabel!
  
@IBAction func loginVerifyButton(sender: AnyObject) {
}

If you know how to link items by using CTRL + Drag to the ViewController.swift file then please use that method. This will just make sure we don’t have to link them up later.

Providing that you have created the start up project correctly from Connecting your Swift Application to the Parse.com Service then this part should be relatively quite simple.

In your @IBAction func loginVerifyButton(sender: AnyObject) { function, the first thing we are going to do is get the input value of those three UITextFields and assign them to a variable. We do this like so:


var usrEntered = usernameTextField.text
var pwdEntered = passwordTextField.text
var emlEntered = emailTextField.text

So now, even though you wont see anything if you were to run your app, when the user clicks the Signup button, those variables will be assigned a value. Hang on! What if the user doesn’t enter any information and just presses the Sign up button? Will it crash? I’m glad you asked, it shows you’re thinking like a developer. For this step, no it will not. The value will just be empty (nil) of those variables. We do need to check however that something has been assigned in order to sign the user up. The fields username, password and email that will be created by Parse are required so if any of these are left empty Parse will have a tantrum and spit you back an error. To get round this we can simply use an if-else statement to check they have some value. This is achieved like so:


if usrEntered != "" && pwdEntered != "" && emlEntered != "" {
    // If not empty then yay, do something
} else {
    self.messageLabel.text = "All Fields Required"
}

Now, When the user clicks the button. The if part of the statement will check whether all the fields are not equals to (!=) to nil. If they all have values then the code in that statement will execute. The else part of the statement catches all the false returns. So if all of those fields are empty or even just one of those then the statement will change the message label that we added earlier to “All Fields Required”.

Go ahead, give it a go, run your app and see.

Create users with PFUser and Swift

We now need to create a new function that will execute this and assign a new instance of PFUser. Just under the @IBAction func loginVerifyButton(sender: AnyObject) { create your function. Like so:


func userSignUp() {
}

So, to create a new instance of PFUser in Swift we need to use var user = PFUser() and also assign the values for the username, email and password we want to store on Parse. You should now have:


var user = PFUser()
user.username = usrEntered
user.password = pwdEntered
user.email = emlEntered

As you can see, we are taking the variable usrEntered (which is the value of the textfield) and assigning it to user.username. We are doing this for all three of the fields.

Parse have provided us with this beauty signUpInBackgroundWithBlock. We implement this like so:


user.signUpInBackgroundWithBlock {
    (succeeded: Bool!, error: NSError!) -> Void in
        if error == nil {
           // Hooray! Let them use the app now.
           self.messageLabel.text = "User Signed Up";
        } else {
            // Show the errorString somewhere and let the user try again.
        }
}

Class Reference for signUpInBackgroundWithBlock

Now, go back to the if-else statement we created earlier and in the part that will run add in userSignup(). That entire if-else statement should look like this now:


if usrEntered != "" && pwdEntered != "" && emlEntered != "" {
    userSignup()
} else {
    self.messageLabel.text = "All Fields Required"
}

That’s it. Your app is now complete. Run it and check that when you type in data into all three fields and signup that you get the message: “User Signed Up”.

Check if PFUser worked

If you have followed the previous tutorial you would have seen where the data stores on Parse.com.

parse-com-core-menu

There should be a new class in there will all the details of the signups. Go have a look. See it?

Thanks for reading this tutorial. The next ones are going to cover interacting with Parse further, checking whether a user has already signed up and then log them into the app. Feel free to comment and share.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


iOS-Blog Admin Team

Written by:

We're here to help.

Comments

comments