Categories
ios swift

User Signup with Parse for IOS

I have used Parse for several projects and It’s reasonably priced. This simple example is about using the PFUser object to sign up a new user to your application.

I will assume you have the Parse IOS Swift SDK installed in your new Xcode project.


1. Make sure you have your provided API Keys setup in the AppDelegate.swift

import Parse
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        Parse.setApplicationId("API Key", clientKey: "Client Key")
        return true
    }

..

2.  In our ViewController.swift we will create some @IBOutlets to some text fields.

@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!

3. We will create an @IBAction that links to a button

@IBAction func signUpBtn(sender: UIButton) {
// Check to make sure the username field is not empty
        guard let username = self.usernameField.text where username != "" else{
            print("Username is required") // Print error for now
            return
        }
// Check to make sure the email field is not empty
        guard let email = emailField.text where email != "" else{
            print("Email is required") // Print error for now
            return
        }
// Check to make sure the password field is not empty
        guard let password = passwordField.text where password != "" else{
            print("Password is required")// Print error for now
            return
        }
        
// If all fields contain values then we call the method below and pass in those values.
        signUpWith(username, email: email, password: password)
    }

 4.  The signUpWith(username, email, password) method

    func signUpWith(username: String, email: String, password: String){
        let newUser = PFUser()
        newUser.password = password
        newUser.email = email
        newUser.username = username
        
        newUser.signUpInBackgroundWithBlock { (success: Bool, serviceError: NSError?) -> Void in
            if let error = serviceError {
// If there was an error, it will be assigned to the [error] constant.
                print("Please try again")
            } else {
                if success{
// If the signup is successful it will return a Bool value, which we assign to [success]
                    print("New user is signed up! Yay!")
                }

            }
        }
        
    }

If all goes well, then you would see a new user in your Parse dashboard for the User class.

Screen Shot 2016-01-19 at 1.43.11 PM