iOS
Sign-up/Sign-in - ObjC
17 min
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 https //www back4app com/product/parse server through back4app this is how it will look like at any time, you can access the complete project built with this tutorial at our https //github com/templates back4app/ios install sdk to complete this quickstart, you need https //developer apple com/xcode/ an app created at back4app follow the https //www back4app com/docs/get started/new parse app to learn how to create a parse app at back4app an ios app connected to back4app note follow the https //www back4app com/docs/ios/parse objc sdk to create an xcode project connected to back4app a paid apple developer account 1 set up 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 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 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 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 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() drag a uibutton on to loggedinviewcontroller in the main storyboard set the button title to ‘logout’ it should look like this 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 run your app and create a couple of users, also try logging in again after registering them login at 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!