iOS
Sign-up/Sign-in - Swift
13 min
login and user registration tutorial using swift introduction this section explains how you can create an app with a simple user registration using parse server core features through back4app at any time, you can access the complete project built with this tutorial at our github repository to complete this quickstart, you need xcode an app created at back4app follow the new parse app tutorial to learn how to create a parse app at back4app an ios app connected to back4app note follow the install parse sdk (swift) tutorial to create an xcode project connected to back4app a paid apple developer account let’s get started! following the next steps you will be able to build an app that will sign in, sign up and logout at back4app database 1 set up and create your sign up and login ui go to xcode, access the main folder from the project and then open the viewcontroller swift file to edit it in viewcontroller swift make sure to include the parse module by including it at the top of the file 1 import parse 3\ go to main storyboard, 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 drag one more loader indicators on each button 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 4\ 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 swift 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… it should look like this 1 import uikit 2 import parse 3 4 class viewcontroller uiviewcontroller { 5 6 @iboutlet weak var txtusernamesignin uitextfield! 7 @iboutlet weak var txtpasswordsignin uitextfield! 8 @iboutlet weak var indicatorlogin uiactivityindicatorview! 9 10 @iboutlet weak var txtusernamesignup uitextfield! 11 @iboutlet weak var txtpasswordsignup uitextfield! 12 @iboutlet weak var indicatorsignup uiactivityindicatorview! 13 14 override func viewdidload() { 15 super viewdidload() 16 // do any additional setup after loading the view 17 } 18 19 @ibaction func signin( sender any) { 20 //todo 21 } 22 23 @ibaction func signup( sender any) { 24 //todo 25 } 26 27 } 2 create a sign in function add the following code inside the sign in function 1 @ibaction func signin( sender any) { 2 pfuser loginwithusername(inbackground self txtusernamesignin text!, password self txtpasswordsignin text!) { 3 (user pfuser?, error error?) > void in 4 if user != nil { 5 self displayalert(withtitle "login successful", message "") 6 } else { 7 self displayalert(withtitle "error", message error! localizeddescription) 8 } 9 } 10 } 3 create a sign up function add the following code inside the sign up function 1 @ibaction func signup( sender any) { 2 let user = pfuser() 3 user username = self txtusernamesignup text 4 user password = self txtpasswordsignup text 5 6 self indicatorsignup startanimating() 7 user signupinbackground {(succeeded bool, error error?) > void in 8 self indicatorsignup stopanimating() 9 if let error = error { 10 self displayalert(withtitle "error", message error localizeddescription) 11 } else { 12 self displayalert(withtitle "success", message "account has been successfully created") 13 } 14 } 15 } 4 log out when logging, it 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 to do logout follow the steps below go to main storyboard, drag one uibutton called “logout”, and add a action between this uibutton and the viewcontroller swift add the following code in this function 1 @ibaction func logout( sender any) { 2 pfuser logout() 3 } 5 application code 1 import uikit 2 import parse 3 4 class viewcontroller uiviewcontroller { 5 6 @iboutlet weak var txtusernamesignin uitextfield! 7 @iboutlet weak var txtpasswordsignin uitextfield! 8 @iboutlet weak var indicatorsignin uiactivityindicatorview! 9 10 @iboutlet weak var txtusernamesignup uitextfield! 11 @iboutlet weak var txtpasswordsignup uitextfield! 12 @iboutlet weak var indicatorsignup uiactivityindicatorview! 13 14 @iboutlet weak var btnlogout uibutton! 15 16 override func viewdidload() { 17 super viewdidload() 18 } 19 20 @ibaction func signin( sender any) { 21 pfuser loginwithusername(inbackground self txtusernamesignin text!, password self txtpasswordsignin text!) { 22 (user pfuser?, error error?) > void in 23 if user != nil { 24 self displayalert(withtitle "login successful", message "") 25 } else { 26 self displayalert(withtitle "error", message error! localizeddescription) 27 } 28 } 29 } 30 31 @ibaction func signup( sender any) { 32 let user = pfuser() 33 user username = self txtusernamesignup text 34 user password = self txtpasswordsignup text 35 36 self indicatorsignup startanimating() 37 user signupinbackground {(succeeded bool, error error?) > void in 38 self indicatorsignup stopanimating() 39 if let error = error { 40 self displayalert(withtitle "error", message error localizeddescription) 41 } else { 42 self displayalert(withtitle "success", message "account has been successfully created") 43 } 44 } 45 } 46 47 @ibaction func logout( sender any) { 48 pfuser logout() 49 } 50 51 func displayalert(withtitle title string, message string) { 52 let alert = uialertcontroller(title title, message message, preferredstyle alert) 53 let okaction = uialertaction(title "ok", style default) 54 alert addaction(okaction) 55 self present(alert, animated true) 56 } 57 58 } the application ui will be similar to this 6 test your app run your app and create a couple of users, also try logging in again after registering them login at back4app website https //www back4app com/ find your app and click on dashboard dashboard > core core > browser browser > user user try logging in and out with the same user and signing back in at this point, you should see your users as displayed below note using the codes displayed above, every time you log in with a user, a session session is opened in your dashboard dashboard , but when the user logs out that particular session session ends also, whenever an unsuccessful login or sign up attempt occurs, the session session opened in parse server dashboard 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!