In this course, we're going to build a Foursquare Clone using Parse. Parse provides you with a great tool to work in cloud servers and you will learn the advantages and disadvantages that come with it.
This course is designed for anyone who wants to:
- Learn about iOS development and coding
- Move into a career as an iOS developer
- Master Swift skills
To get the most out of this course, you should have some basic knowledge of iOS.
Hi. Within this lecture, we're going to see how we can create users using Parse User Class. So, let me come over here and actually, rather than just commenting out this, I will just comment out everything so that I won't just update or retrieve any data using Parse Server right now. I'm going to head back to my Main.storyboard because we need some text fields to get user input here to create user accounts. So, I'm going to bring in two text fields and two more buttons later on. I'm going to ask for username and the password. So, let me bring in one label first because I'm going to have a title in my own app. So, this is going to be something like Foursquare Clone and this will act as our title or our heading. So, I'm going to go for Foursquare Clone and I'm going to make this a little bit bigger. So, where is the font? Here it is. So, this is okay, right? And under this header or under this title, I'm going to go for text fields. So, let me make this a little bit bigger. So, the first one will be for username, and in Parse you can just create users with username rather than only email. You can get email if you want but I'm going to show you how to do this with password and username only and you will know how to do this with email as well, don't worry. Username, password, so let me bring in one button over here. So, this will be something like Sign In button so that if our user has an account they will get to sign in from this button. I'm going to copy and paste this button because I'm going to need another button called Sign Up. So, if user doesn't have any account registered previously, they will get to create one using this option. So, let me see if this is aligned, they were aligned but I've totally wrecked it. So, let me come over here and say Reset to Suggested Constraints so that we can move onto our view controller and define all these views. So, let me go back to Mainstoryboard over here. So, this will be my username text. So, come over here and say userNameText. Also I'm going to need a password text as well, so this one, passwordText. And we have two buttons, I'm going to bring them in as an action, so let's do the Sign In first. Let's bring in Sign In and say signInClicked. So, this is an action, and we're going to do that for Sign Up as well. So, this is signUpClicked. Here you go. Now, let me close this down and go back to my view controller. So, in my view controller in fact I'm going to delete everything from here because we get to practice this reading and writing thing a lot and you will have your notes, don't worry. In the signInClicked, I don't need to do anything right now because we have to start with signing up first. We don't have any prior accounts in that moment. First we need to create one that we can sign in using that account and using authentication in Parse is very easy. You call Parse user class just like we did authentication in the Firebase. Remember, we have called off that auth in Firebase. Now, let me start by checking to see if username text is not empty and password text is not empty. If they are indeed empty then I will display some alert message to the user saying that you need to fill out some username and password. I'm going to create a make alert function over here. Remember, we are going to use this alert during this ViewController.swift file progress and we are going to need this function alert. So, I'm going to go for alert. This is going to be a UIAlert, not action actually, controller. So, UIAlertController. If your parenthesis this will ask you for a title and message and a style. Of course, I'm going to give title input as a string in here and the message input as string over here so that I can come over and say this is going to be a title input and this is going to be a message input. So, it will display our title and messages we want and the style will be .alert. Since we have done this alert before, so I'm doing this a little bit fast. So, this is going to be an addButton like UIAlertAction and title will be okay and this is not going to be an add button, this is going to be OK button. And the style will be default and we don't need any handler, and then I'm going to add this action to my alert by saying alert.addAction and finally I'm going to go for self.present. I'm going to present this alert with an animation of course and the completion, we don't need any completion. Right now, if you come over here you can just say make alert and for a title you can just go for error and if they're empty you can just say something like give me username and password. If you don't give me, how can I create an account for you. If they are correct, if there are not empty strings, then I'm going to call my Parse user class like I have said before because that's where I manage the authentication procedures like signing in and signing up. So, I'm going to say let user be a PFUser. As you can see this is what we want in order to go with user management. And I'm going to give a password and the username to that user. As you can see you get this email option as well, if you want you can sign them up with email but I'm going to go for username because in Firebase we don't have this kind of option. So, this is maybe some kind of an advantage compared to the Firebase. So, I'm going to go with the username. So, let me see this is password, we have other methods here. So, I want username, this one. And I'm going to take this username from userNameText.text and I'm certain that this is not empty right now, so I can put some exclamation mark at the end of this text. I'm going to do this for the password as well, right? After I give the username and the password, I can just say sign up. So, if you say sign, you will see some options. Like if you go for sign up, it does it synchronously, sign up in background with block, of course with what we want, okay? So, this is again synchronously signed up in background. Not synchronously but asynchronous even though we don't get any block, but what we want is the general one sign up in background with block. So, if you hit Enter, it will give you a Boolean indicating whether it is a success or failure. So, I'm going to say success and error. If error is not nil, I'm just going to display an alert message. So, I'm going to use my make alert function and for a title I'm going to go for error and for message input, I'm going to use this error that is given to me. So, far so good. So, this asks us to provide a default value like before. So, if this error message is empty, I'm going to go for error and else. Else meaning the user has been created. We get another message here indicating that that we want to have a self over here because we are in a closure and that's right. So, else, I want to do a segue here, right? I just want to take user to the other view controllers. But right now I don't have any view controllers so I'm just going to take a note saying segue, and maybe we can do something like printing out, this is created. So, let me say okay for right now. And let me run this to test actually because we are ready to test to see if we can create users with passwords and usernames. So, let me open my simulator, come over here and say username is James and password will be 1, 2, 3, 4, 5, 6. If you say Sign Up, here you go, we see the okay. Now, let's go back to our Parse User Class and let me refresh this, and here you go, we have our user. So, we have an object ID and we have the username as James. We have the password but it's hidden default so we cannot see it and we didn't provide any email here. So, my suggestion is to get emails when you deal with a real app so that you can use their emails afterwards if you need them, and you know how to do that already. So, let me go back and do something like empty string. And here you go, we have the alert message. So, our function actually works. So, that's good. We have created our user. We're going to stop here and within the next lecture we're going to see how we can sign this user in.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.