iOS
...
Users
User Registration
9 min
user registration introduction most real world applications often utilize user based features to provide a more personalized service to clients these functionalities require the client to sign up on the app with the back4app platform and the parseswift sdk parseswift sdk , you will be able to implement those features in your apps simply and quickly prerequisites to complete this tutorial, you will need an app created on back4app a basic ios app to test queries goal to implement a user registration feature on an ios app using the parseswift sdk parseswift sdk 1 understanding the user registration flow in order to integrate a signup option on an ios app, it is necessary to create an object that conforms to the parseuser parseuser protocol this protocol implements the main required properties so that back4app is able to store and manage login information the following snippet shows how a user object can be implemented 1 import foundation 2 import parseswift 3 4 struct user parseuser { 5 // additional properties required by the parseuser protocol 6 var authdata \[string \[string string]?]? 7 var originaldata data? 8 var objectid string? 9 var createdat date? 10 var updatedat date? 11 var acl parseacl? 12 13 // main properties linked to the user's information 14 var username string? 15 var email string? 16 var emailverified bool? 17 var password string? 18 19 // a custom property 20 var age int? 21 } as it can be seen in the above snippet, parseswift parseswift allows us to have a very flexible implementation for a user object similar to the age age field, we can add as many additional fields as needed once we have the user user object ready, the parseuser parseuser protocol gives this object a set of methods to handle all the necessary user operations such as sign up , log in , log out , etc in the following step, we first take a look at how to implement a signup request 2 creating a signup request we start by adding the corresponding form where the user enters their account information let viewcontroller viewcontroller (a subclass of uiviewcontroller uiviewcontroller ) be the class where we implement the form in the snippet below, we remark the key elements a basic signup form should have 1 import uikit 2 import parseswift 3 4 class viewcontroller uiviewcontroller { 5 // user inputs 6 @iboutlet weak var usernametextfield uitextfield! 7 @iboutlet weak var emailtextfield uitextfield! 8 @iboutlet weak var passwordtextfield uitextfield! 9 10 // sign up button 11 @iboutlet weak var signupbutton uibutton! 12 13 override func viewdidload() { 14 super viewdidload() 15 16 // add additional code if needed 17 } 18 19 // called when the user taps on the signupbutton 20 @ibaction func handlesignup( sender any) { 21 guard let username = usernametextfield text, let password = passwordtextfield text else { 22 return showmessage(title "error", message "the credentials are not valid ") 23 } 24 25 signup(username username, email emailtextfield text, password password) 26 } 27 28 // this method prepares and registers the new user 29 private func signup(username string, email string?, password string) { 30 // todo here we will implement the signup action 31 } 32 33 // presents an alert with a title, a message and a back button 34 func showmessage(title string, message string) { 35 let alertcontroller = uialertcontroller(title title, message message, preferredstyle alert) 36 37 alertcontroller addaction(uialertaction(title "back", style cancel)) 38 39 present(alertcontroller, animated true) 40 } 41 } we leave the layout part to the developer you can integrate and set up the visual components according to your needs next, in the following step, we implement the signup(username\ email\ password ) signup(username\ email\ password ) method 3 implementing the signup function the first step for signing up a user is to have an instance of a user user object with its corresponding credentials the username username and the password password fields are mandatory to register a new user, the remaining fields are optional therefore, a typical way to instantiate a user user object would be 1 var newuser = user(username "acoolusername", email "myemail\@domain net", password "mysecurepassword") 2 newuser age = 25 additionally, we should also provide the initial values for the custom fields, like the age age field in our case the next step is to perform the signup action the parseuser parseuser protocol implements the method signup( ) signup( ) that will allow us to send the signup request to the back4app application there are three types of implementations for signup( ) signup( ) depending on the use case, one should pick the appropriate one now, we can complete the signup(username\ email\ password ) signup(username\ email\ password ) in viewcontroller viewcontroller 1 class viewcontroller uiviewcontroller { 2 3 4 private func signup(username string, email string?, password string) { 5 var newuser = user(username username, email email, password password) 6 newuser age = 25 // warning this should be entered by the user, not hardcoded 7 8 //warning only use one of the following cases, the synchronous or the asynchronous case 9 10 // the below registers the user synchronously and returns the updated user object (stored on your back4app application) 11 do { 12 let signedupuser = try newuser signup() 13 showmessage(title "signup succeeded", message "\\(user)") 14 usernametextfield text = nil 15 emailtextfield text = nil 16 passwordtextfield text = nil 17 } catch let error as parseerror { 18 showmessage(title "error", message error message) 19 } catch { 20 showmessage(title "error", message error localizeddescription) 21 } 22 23 // the below registers the user asynchronously and returns the updated user object (stored on your back4app application) wrapped in a result\<user, parseerror> object 24 newuser signup { \[weak self] result in 25 switch result { 26 case success(let signedupuser) 27 self? showmessage(title "signup succeeded", message "\\(signedupuser)") 28 self? usernametextfield text = nil 29 self? emailtextfield text = nil 30 self? passwordtextfield text = nil 31 case failure(let error) 32 self? showmessage(title "error", message error message) 33 } 34 } 35 } 36 } note registering a new user using the signup( ) signup( ) method automatically logs in the user, so there is no need for the user to log in again to continue using your app at any time during your app’s lifecycle, you can have access to the currently logged in user from a static property implemented in the parseuser parseuser protocol 1 let loggedinuser user? = user current in this repository you can find a simple user registration app that follows the steps we detailed above conclusion the back4app platform together with the parseswift sdk parseswift sdk offers a quick and staightforward way to integrate a signup flow onto your ios apps furthermore, in the following guides, we will explore the remaining procedures like log in, log out, etc