Sign-up/Sign-in - Swift
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:
- An app created 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.
Following the next steps you will be able to build an App that will Sign-in, Sign-up and Logout at Back4App Database.
- 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.
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:
Add the following code inside the sign-in function:
Add the following code inside the sign-up function:
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:
The application UI will be similar to this:
- Run your app and create a couple of users, also try logging in again after registering them.
- Find your app and click on Dashboard>Core>Browser>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 is opened in your Dashboard, but when the user logs out that particular Session ends. Also, whenever an unsuccessful login or sign up attempt occurs, the Session opened in Parse Server Dashboard is deleted.
At this stage, you can log in, register or log out of your app using Parse Server core features through Back4App!