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.
Intended Audience
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
Prerequisites
To get the most out of this course, you should have some basic knowledge of iOS.
Hi, within this lecture, we're going to find a way to remember the user so that every time we open the app it won't ask us for a username and password if the user has been previously logged in, it will just take the user to the places view controller. So, what we're going to do is the same thing that we did in the Firebase section. So, we have used this sceneDelegate, maybe you remember we have used this will connect to session function. So, that's what we're going to do in this section as well. But before we go on and do that, I'm going to show you if you do that under viewDidLoad in the viewController, it won't work again. So, let me show you how this goes. If you create a user called currentUser. And if you say PFUser.current. So, this gives the currently logged in user and returns the instance of it.
So, if this is not nil, it means that there is a currentUser. So, if you say currentUser, you get a PFUser. And if this is not nil again, then you can actually see the email or username or password of this user. So, let me run this and let's see if we can get this user in our logs. So, let's come over here, and as you can see we can see the James as optional. So, James has been previously logged in. Even though this is logged in we cannot take user to the places viewController. So, can we do something like if currentUser is not nil take us to the places viewController by doing segue. And no, we cannot do that actually because it won't work. And as you can see, let me write we can identifier to places we see and try to perform this segue. So, once I do that it won't take me to the places viewController because we have to do that under seen delegate before everything loads up. And we have to change the route viewController. That's what we did in the Firebase section and it doesn't make sense to do a segue here. It doesn't make sense to load this viewController at all. So, best way to do that is to check this under sceneDelegate. And if that's the case, then change the route viewController. So, let me cut everything from here and come over here under our sceneDelegate. So, first of all, in order to use this class, I'm going to import parts in here as well, then PFUuser won't give me an error. This will give an error because we cannot use performSegue in sceneDelegate. We don't have any viewController in here.
Rather, I'm going to create my UIStoryboard actually as a variable. So, the name of this storyboard is main. I'm going to create a variable, representing our PlacesViewController so that I can instantiate that viewController. So, I'm going to say board.instantiate instantiateViewController. And it asks us for an identifer. So, I don't have any current identifiers because we haven't give them one. So, if you come over here to placesViewController you can give them. But in fact you don't want to give any identifier to placesViewController. You want to do that in navigationViewController. So, choose navigation viewController and say something like navigationController as a storyboard ID. And remember the class here is UINavigationController. It doesn't have any specific .swift files. So, we're going to cast this as a navigationController. So, in identifier I'm writing the exact same thing that I have written there. And I'm going to first cast this as UINavigationController.
And since this is actually a UINavigationController, it won't fail. So, rather than calling this placesVC, I'm going to call this navigationController in order to avoid any confusion. And in here I'm going to call the window as a rootViewController and say navigationController. So, what does it do? It takes this entry point inside in front of this navigationController so that we can change the launching viewController. So, what we're going to see when we first launch the app is going to be the placesViewController if and only if the user has been previously logged in. So, here you go, now we see the placesViewController. And this works. But this time we got another problem, we have to have a log out button here so that user can log out anytime they want. So how do we do that? Let's go to placesViewController and we're going to add another navigation bar item. This time it's going to be a left bar item rather than right bar item.
So, I'm going to say log out here and if this works, then I'm going to go back to my sign in viewController. So, let's go to placesViewController and let's add a new button over here. Let's call navigationController.navigationBar .topItem.leftBarButtonItem. So, this is leftBarButtonItem. So, this is going to be on the left rather than right. This is going to be the only difference. And actually the other difference will be the style of this button. So, I'm not going to go for a bar button system item this time because I just want to have a title in my button.
So, I'm just going to give a specific value, something like log out. So, I'm going to choose this one with title. So, this asks us for a string and string will be Logout and style again, I can just choose something like done or plain. This is regular style. Let's go for plain for example and let's see if this looks good and if it doesn't, we can always come back here and change it. Target will be self, for action let me create another function called logoutButtonClicked. So, that we can add this as a selector over here. So, this will be selector and logoutButtonClicked. Here you go. Now, what we're going to do here is to log out from the parts and then bring the user to our sign up viewController.
So, let me import the parts first of all and we're going to take the user to the viewController.swift. So, for logging out, I'm going to use PFUser.logout. And again we have some options over here. I'm going to go for logOutInBackground with block, so that if I have any error, like losing the Internet connection or something, I just show it to the user. And let me choose this and hit 'Enter' it will give me error, if there is any error, I'm just going to show it to the user. If there is not, it means that it worked out well. So, let me create the error here and we don't have any alert messages right now. So, I'm going to create one. Let's do this fast. So, this is going to be an AlertController with title error, message error.localizedDescription. Style will be enter.styleAlert. So, the next thing will be for me to create an okButton.
This is going to be an action. And for title I'm going to say OK. And for style I'm going to go for default and I don't need any handler. I'm going to add this to my alert as an action and I'm going to display this alert with self.present and then I'm going to give alert here with animation without completion. And if that's not the case, if there is not any error, it means that I get to go from here to my viewController. And in order to do that, I don't have any method right now. Because I cannot use navigationController like we did before. So, in here I have to come from here to my sign in viewController. So, let me do a show segue. And if I do a show segue it gives us a navigationBarBotton, which I don't want. I don't want user to go back from sign in viewController to here. So, I'm going to present this modeling and I'm going to do this full screen. So, that's good.
And I'm going to give an identifier to sign up, View C. And this is not named SignUp right now, I'm going to change its name later on. But for right now I'm going to copy this and come over here and say self.performSegue, For identifier I'm going to say to SignUp VC and sender will be nil. So, let's test this. Let's run this in our simulator. It should take us to the placesViewController automatically because we are already signed in. And here we go. Now, we see the log out button. If I click over here, it takes me to my SignUp VC. Now, let me run this from scratch and if it takes us to the SignUp VC then it works fine. And here you go, it works fine because we have already been logged out. Let's run in with James 1, 2, 3, 4, 5, 6. Here we go.
Now, let's run it from scratch one more time to see if we see placesVC. Here you go, now we are in. So, sign in, sign up, log out, everything seems to be working pretty good. So, we are done with our authentication procedures. So, let me rename this. In fact there are a couple of ways to rename this and I'm going to show you the easiest one. Okay, you can just click over here to viewController to rename this file manually, but then it means that you have to change everything manually. Most basic way to do that is to come over here and right click and say Refactor, Rename. So, when you do that it will change everything regarding to viewController.
So, it will rename every line that we are using this viewController in. So, if you want something like sign up then you're more than welcome to do it and say rename. Once you say rename it will change the class name from here, from here. And if you're using it anywhere in the project, it will change it as well. So, best way to rename variables or classes or any object is to right click on it and say Refactor, Rename. So, when I run this on my simulator, everything seems to be working, so it didn't break anything. We are done with our authentication now we need to move on. First of all, I'm going to create a user interface. I'm going to bring in all my viewControllers, so that we can start working on them finally. Let's do that in 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.