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 write our Save button and clicked function because we want to save all this information into our past database. So, now we have everything.
Let me import the parts here so that we can create a PFObject. And we're going to have to just create our object and assign the values to its keys. So, I'm going to say this will be a PFObject and className we're going to have to choose a new className because I believe we don't have any current class. So far, we have been working on these fruits. Of course, I don't want that. I'm going to create a newClass called places with a C places. And with this object I'm going to say object ["name"], right? So, we're going to have a name. This will be our place name. And this can be easily gotten from the placeModel. So, let me create a placeModel over here. It will be placeModel.sharedInstance, okay? So, if we come over here and say, placeModel.placeName, we can actually reach the placeName. So, let's go for type placeModel.placeType, right? And again, the atmosphere. So, atmosphere place Model.placeAtmosphere this one and we're going to have the image, but I'm going to do that later on. I'm going to go for latitude and longitude. So, we have this latitude and longitude, right? So, how can I get this latitude? Can I get it from placeModel.? No, I cannot get that because I didn't actually give that property to my placeModel singleton. And I can't just come over here and say that chosenLatitude. And for the longitude, I can come over here and say longitude is actually self.chosenLongitude. But I believe if we are doing something with a model, we have to be consistent rather than getting this from chosenLatitude and chosenLongitude, why don't we define these attributes in the singleton class so that we can assign its values inside of our mapVC? So, let's call this placeLatitude and call this placeLongitude, okay? And by the way, the other method will work as well. I'm just trying to be consistent here. If we're doing this with a model, then I believe we should stick to the model and we should have all the attributes inside of that model, okay? So, that's why I'm adding this placeLatitude and longitude here. So, rather than using this self.chosenLatitude, I'm going to go for our placeModel.sharedInstance.placeLatitude, okay? And for here I'm going to go for placeModel. Again, placeModel.sharedInstance.placeLongitude. And this will be equal to strings. So, let me delete all of this actually. We're not going to be using them. And in here I'm going to go for a placeModel.placeLatitude. And in here I'm going to go for a placeModel.placeLongitude. So, here you go. Now, all we have to do is just add the image. So, how do we add the image? Of course, we're going to convert it to data as we did in the Firebase, as we did in the Core Data. So, if we try to upload an image, we have to change it into a data first so that we can actually upload that data. So, I'm going to say, if let image data and this will be coming from placeModel.placeImage. So, this is a UIImage, and we're going to convert it into a jpegData with a compression quality of 0.5, so that I can come over here and say object ["image"] is the PFFile, okay? So, this is going to be a PFFile and this PFFileObject will ask us for a data and I'm going to choose this one name and data. For name, I'm going to call this image.jpg, and for data I'm going to give imageData. And here you go. Now you know how to upload data to the Parse server as well. So, now I have my name, type, atmosphere latitude, longitude, and image and I believe that's all we need to create a place.
So, let's say object.save and remember we use saveInBackground with block. So, if you hit 'Enter' on that block, it will give you a Boolean indicating if this is success or not. And I'm going to check to see if error is not nil. I'm going to print out an error and else I'm going to do a segue, right? I'm going to go back to our placesVC. So, I believe we don't have that segue right now, so we have to go to main storyboard. And let's create that segue because we cannot go to placesVC right now. So, let me choose here to our placesVC and say, 'Show segue.' And once I do that, it distorts the view. So, I'm going to go for present modelaly rather than show and I'm going to choose fullscreen. So, here we go, let me close this down. no. Let me just give some identifier. So, I'm going to call this fromMapsVC to PlacesVC because we already have something called toplacesVC, right?
So, let me copy this identifier and paste it in here and sender will be nil, so far so good. And let's see if we have any alert messages in here, nope, we don't have. So, we're going to have to create it from scratch. I'm going to call this UIAlertController and title will be error message will come from the error itself error.localizedDescription. And the style will be error and this is not this is alert, and I'm going to create an okButton, this will be an UIAlertAction and title will be "OK". Style will be default and handler will be nil. I'm going to add this to my alert "OK" as an action and then I'm going to present this present alert animated, true completion nil. So, this is good. Let's try this in our simulator and let's see if we can create our first ever Place.
So, here we go moment of truth, let's see if we can actually upload something to the Parse server. So, let me actually come over here and close this down to find some real restaurant or cafe images so that we won't try this with Mona Lisa, right? Let's go for a restaurant and I'm going to just bring in the first couple of images to use it on our app. So, I'm going to save this to my photos. So, let's find another restaurant, and everything looks like the same, but I'm going to go for this one, okay? And maybe we can search for some cafes or bars. Let's go for a bar. Okay, and let's choose this one. Let's bring this in add to photos and let's find another one. This one is good, but I cannot seem to get it, I'm long pressing, but it doesn't work. So, let's go for another image. This one maybe yes, this can be added. Here you go. Now let's go to our Foursquare clone.
I'm going to give some real datas right now. And by real, I mean not gibberish. James restaurant, type will be restaurant and the atmosphere will be nice. Let's choose one of the images that we have downloaded and let's click on 'Next' and let's choose a location from the map. So, let's go for lights a plane for this time. Let's go for Apple store over here And long press for three seconds. Here you go. We see the James restaurant. Now, if I say "Save" not now, it will save all this information, but we cannot see that and we cannot see the Add button as well. So, there is some sort of a bug over here, we're going to have to figure out it later on. We lost the UINavigation Bar. We're going to take care of that later on. Don't worry; just for right now, I just want to make sure that if we manage to create our placesClass. So, let me refresh this page in my Parse dashboard and here you go. We have the Parse placesClass. Now we see the name, we see the latitude, we see the longitude, we see the type, and we see the image as well. So, let me try to download this. Here you go. The image is uploaded, so everything seems to be working fine. Now, we're going to have to find a way to get this data and display it in our TableView to the users so that user can tap one of them and see the details in another View Controller. 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.