In this course, we're going to create a Snapchat clone using structs and various advanced techniques of Firestore.
- Learn how to make a Snapchat clone
- Understand all the features of Snapchat and how to set them up
This course is intended for anyone who wants to learn how to develop apps on iOS.
To get the most out of this course, some basic knowledge of iOS development would be beneficial. We recommend that you take this course as part of the Developing Mobile Apps for iOS learning path.
Hi, within this lecture we're going to take care of our signing in and signing up process. So, let's go in our SignInVC.swift and we're going to import the Firebase first of all because we're going to use the Firebase modules over here, right? And then we're going to use this passwordText, usernameText, and emailText because we want to check to see if they are empty or not. So, I'm going to say if userNameText.text is not an empty string and if passwordText.text is not an empty string as well, and this time we're going to have the email text as well. In fact, we're going to use userNameText.text for the first time over here because we're going to sign the users up and then we're going to use this username to save their username as well. So, let me create my makeAlert function over here because we're going to need it so much. So, I'm going to take in a title and a message as a string variable, and I'm going to create my alert, so this will be a UIAlertController. This will ask me for a title and I'm going to give the title that I am taking as an input and the message as well, and the style will be an alert. Then I'm creating an okButton and this will be a UIAlertAction. This will ask me for a title which is okay and the style which is default and we don't need any handler. So, later on I'm going to add this as an action to my alert and I'm going to present this okay alert here with an animation and without the completion. I'm doing all of this very fast because we have been doing it for a long time right now. So, for title I'm going to say error and for message, why don't you give me a Username, Password or Email, okay? So, over here, if these are, if these conditions hold, then I'm going to sign my users up. So, I'm going to use auth in order to do that and remember we have to go for createUser. So, when we say createUser it will ask us for an email and a password. So, email will come from emailText.text and I can force unwrap this, and for password I need passwordText.text, right? And for completion block, I will hit 'Enter'. It will give me either an auth data result. I'm going to call this auth and or an error, okay? So, let me close this down. And if error is not nil, then I'm going to display this error to the user. So, I'm going to say self.makeAlert and title will be error and the message will be error.localizedDescription and if this is actually not an error message, I'm going to display only the error message. So, else, if there is not any error, what do I do? I want to perform a segue, actually, okay? ToFeedVC, and of course I'm going to do this eventually anyway, but before we do that, I want to go ahead and save my username to the Firebase Firestore. So, there is no space to save our username in the authentication module, so I'm going to use Firestore. Of course, other than, besides the username, you can just say whatever you want, like user gender user, other specifications, user location, user country, user phone number, but I'm just going to go for username but you will understand how this works. So, first of all, I need to create my Firestore variable, okay? So, this is firestore.firestore and I will use this Firestore to create a collection. So, remember we have worked with posts before. Right now, I'm going to call this UserInfo. After that, I'm going to say userinfo.addDocument. So, this will ask me for a string to any dictionary and I'm going to call this userDictionary in which we will have the email address of the user and I can get this from emailText and why I'm saving this because I'm going to search for our username using this email later on. So, I can get this from auth as well, but since I have this on my user emailText.text it's easier to get it from there. So, emailText.text, okay? And then, later on I'm going to just save the username as well and this will be from the usernameText.text. So, this is the dictionary that I'm looking for. So, come over here and say userDictionary and as you can see this is a string to string dictionary and I'm going to cast this as to be a string to any dictionary, okay? Once I do that, it asks me to put self over here and it's right because we are in a closure, right? And the later on, I believe this will give us a warning saying that, why don't you use just an 'as' rather than first casting this? We can do that. As long as we have string to any dictionary, it doesn't matter for us. And in here I'm going to hit 'Enter' because I'm going to get an error over here and I'm going to check to see if this error is nil or not. So, in this case it's your choice to display any error message to the user. Since you have been doing that already, you may want to do that over here as well or you may just skip it. But at this point the user connection may get lost or something, so maybe you can just go ahead and display this error message over here as well. And if this works well, then it will just save my username to the FireStore. So, let me test this and let's see if we can see the username and email in the fireStore. So, once I hit the runButton you will see some warnings over here which are related to Firebase modules mostly, so I'm going to ignore them and this will take some time. As you can see it's trying to build 9,000 tasks because we have so much task because we have imported Firebase. So, we have to wait until it's finished and then we're going to try our code. And if this works out, then we will see the email and our username in the FireStore. So, let's test this. I'm going to say firstname.lastname@example.org over here and James Hetfield as username and I'm going to give some 1, 2, 3, 4, 5, 6 password. If I hit 'Sign Up' button, it will sign me up and bring me into the feed. So, if I go over here and if I refresh this and we are not in the database, I'm just refreshing the storage. So, let me go into the database, and here you go. Now, we see the UserInfo collection and once it loads we will see the details like document and here you go, we see the email and the username. Now, I can try to get this username by just providing the email and of course in the authentication I have my user as well. Now, it's all good. So, we have our email and username start in the UserInfo collection. Now, we're going to use that later on in the feed View Controller, but right now let me go to sign in View Controller, okay? Because we're going to take care of this signInClicked function as well. So, I'm going to copy and paste this because we're going to check to see if these are empty or not again, and if these are empty we're going to display the same error message. And in here we're going to call the authentication one more time and rather than creating the user, we're going to sign the user in with email and password and completion block. So, email will come from emailText and password will come from passwordText. So, let's say emailText.text, okay? And for password say passwordText.text and force unwrap this. And completion hit 'Enter'. This will give me either a result or an error. So, say result an error if error is not nil, then I'm going to display this error message to the user. So I'm going to say self.makeAlert and title will be error, the message will be error.localizedDescription and if this is empty I'm going to say error else else I'm going to do a segue and I'm going to do the segue toFeedVC, okay? Sender will be nil. And, in fact, at this point we don't need to check to see if the username text is empty or not because we're not going to use this username. We can just sign in the user with email and password even though we're going to use the username in the real app, at this point we just need the email and password if they are signing in. In the sign up, of course, we have to check it because we want to save it to the Firestore. So, let me open it one more time and try to do a sign in from here. So, I'm going to say email@example.com and for password I'm going to give 1, 2, 3, 4, 5, 6 and sign him in. As you can see, now we are signed in and we are in the feed View Controller. Of course, we have to take care of this log out and remember user functions, that's what we're going to do within the next lecture.
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.