1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Leveraging Map Functions for Your iOS Apps

Saving Places

Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 18m
Students
1
Description

This course explores maps and how we can integrate them into our apps. We'll leverage map functions like finding the user location, showing the user's location on the map, and choosing a location on the map to get its coordinates. And in order to do that, we're going to be focusing on an app called Travel Book. We're going to integrate Core Data in this Travel Book as well so that we can re-practice what we have learned in the previous section. 

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.

Transcript

Hi, within this lecture, we're going to save all this information into our Core Data database. So, in order to do that, we're going to need a Save button. We can bring the Save button on the bottom of this page or we can just put it on the top of this page on the Navigation Controller bar like we did before. I'm going to do it on the bottom of this View Controller because it would be more clear. Like, this is step one, the map is step two, and step three will be the Save button. Of course, you can do it on the Navigation bar as well if you want. But, I'm going to go for the bottom of this page. Okay, so I'm going to decrease this height a little bit as well; this time upwards. And I'm going to bring in the button on the bottom of this View Controller. So, drag and drop one button, not here, but at the bottom here. So, let me zoom in and let me make this a little bit bigger so like that and put it in the center of the View Controller. Then, we can call this 'Save'. So, this will be our Save button. Let me open my assistant editor here and let me call the View Controller. Now, we're going to call the Save button as an action in here. So, let me click 'Control' and drag and drop over here and call this 'saveButtonClicked'. Here you go. Let me 'Close' this down. And before we go on, don't forget to add the constraint. So, choose the 'View Controller' and come to the bottom and say, 'Reset to Suggested Constraints'. Let's check if everything has constraints. Yes, it seems so. So, let's 'Run' this and see if we have anything suspicious, if we have anything distorted. So, we can come back and see the constraints. But everything seems to be fine right now so I believe we can move on with this kind of view. We're going to get name, we're going to get the comment, we're going to choose the map and save it. And remember, when we click on this, the keyboard will open in a real app. And we have to make sure that we have necessary code in order to tap anywhere in the View and close down the keyboard. We have learned how to do that. So, I'm not going to implement this all over again because I don't want to make this longer than it should be. So, in a real app, maybe you may want to implement it yourself. So, right now, I'm going to go to 'xcdatamodel'. Okay, I'm going to add a new entity. And this time I'm going to call this places or something, places or locations. Before, we call these paintings. And remember, the entity name can be changed by clicking in here and saying whatever you want, like Places. And in the Attributes, we're going to get the title or the name of the location that we have chosen. And then, we're going to get a command. And then, of course, we need to save a latitude and a longitude. So, let's say, 'title' for the name. And I'm going to call this, it's going to be a String. And then, we're going to have a 'subtitle' and this will be String as well. Later on, we're going to get a latitude and longitude. So, what will be the type of latitude? Is it going to be an integer? Of course not, because we have decimals and decimals actually matter in this case so I'm going to go for a Double, okay? We say, 'latitude' and 'longitude' as Double in here and in the coding section as well. When we create a latitude variable, for example, it should be a type of Double. So, it's the same thing for longitude as well. Let's choose 'longitude', 'Double'. And for the last one, I'm going to go for 'id' and this will be a 'UUID'. If you haven't watched the previous section, I suggest you go back and watch because we have discussed why are we using an ID, what is a universal unique ID, and how do we leverage those ID things in our code. Okay, I'm not going to go over that one more time. So, in the saveButtonClicked, remember, we have to use CoreData, okay. So, I have to import it, 'CoreData', and come down to saveButtonClicked. And first thing we're going to do, we're going to create the appDelegate variables. Remember, we have to use appDelegate in order to reach and use automatically generated CoreData functions for us. And then, we're going to create this context and then we're going to use that context in order to save, delete, or retrieve the values that we want. So, I'm going to say, 'let appDelegate'. It's going to derive from 'UIApplication.shared.delegate'. And later on, I'm going to say, 'let context'. This is going to derive from 'appDelegate.persistentContainer'. Okay, it doesn't show up. Let me see one more time. So, this is going to be 'persistentContainer'. It doesn't show up because I didn't cast this previous value as to be our appDelegate. Remember, we have to force cast this as appDelegate. Now, if I say, 'appDelegate.persistentContainer.viewContext'. So, I'm going over this very fast as we have done this like a billion times right now. I'm not explaining why I'm doing this because we already seen how to deal with Core Data in the previous section. Again, if you haven't watched, please go back and watch the previous section or else you wouldn't understand what I'm doing right now. So, what I'm going to do, I'm going to create a variable called 'newPlace' like we did a newArt before. So, I'm going to call this 'NSEntityDescription' and insert new value, 'insertNewObject', to my entity. And the entity name that I'm looking for is the Places and we're going to use this context in order to save and retrieve information here. So, next thing is very easy. We're going to say, 'newPlace.setValue' and we have to provide a value for some certain keys and keys are pre-defined for us by us, actually. We have defined title, subtitle, latitude, longitude, remember? And we can get this title from nameText.text easily. And we can get the comment subtitle from commentText, right? So, this is going to be 'commentText.text' and the key will be 'subtitle'. Maybe we should have used comment a name for keys as well but since we have defined it, I'm not going to change it later on. The next thing would be to get the latitude and longitude. Right now, I don't have any variables to reach out and get the latitude and longitude. So, what I'm going to do, I'm going to create a chosenLatitude and chosenLongitude variables. And I'm going to define those variables as doubles. And then, later on, when somebody chooses a coordinate from the map in our app, I'm going to get that chosen latitude and longitude from the UI gesture recognizer that we have created, okay? It's fairly easy to do that so we have worked with that kind of operation before. What we want to do, we have to come over here. We already have our touchedCoordinates. so we can get touchedCoordinates.latitude, touchedCoordinates.longitude, and assign those values to newly created variables like touchedCoordinates.longitude, latitude. As you can see, we can reach those variables, okay? So, let's create our variables as doubles. So, let's go to our 'class' and let's say, we're going to use 'var' because we're going to change this after all. So, I'm going to say, 'chosenLatitude = Double', okay. And I'm going to say, 'chosen.Longitude = Double'. So, this will be our latitude and longitude variables. And then, in here, we can just say, 'chosenLatitude = touchedCoordinates.latitude'. So, 'chosenLongitude = touchedCoordinates.longitude'. So, that's perfect, that's what we wanted. So, we are creating this chosenLatitude and longitude. From here, we are assigning new values under our chosen location function. And when the Save button gets clicked, we can just put those information into our Core Data model. Of course, I believe, in order to be safe, maybe you can check for if nameText is empty, if actually an annotation has been created or not. You can have different kinds of checkpoints for that but I'm not going to go into that kind of operation because we have already seen how to do that before, right? And the last thing to be add is actually the ID itself. And here, we already have created ID and the type will be UUID. So, it's very easy to do that because Xcode takes care of this when we write UUID, okay? And it means that we have completed the setting value process. Now remember, we can just say, 'context.save', in order to save this into Core Data. And when we say that, it throws an error. So, we have to do that with do-try-catch structure. So, 'do try context.save', and we're going to catch if there is any error, okay? So, let me cut this from here and paste it in here. And if we have a problem, we're going to just print out an error. So, if we don't have any problem, we're just going to print out success. Right now, even if this works, we don't have any method to see if it works or not because we're not retrieving the data yet. So, I'm printing out success just to test it. So, here you go. Let me just go zoom in in the Eiffel Tower a little bit and we're going to save this. So, when I click over here without giving name and comment, it will bring me a new annotation but it won't have any name and comment. For this kind of thing, I believe you should check if nameText.text is empty, commentText.text is empty but I will assume that you can do it on your own. So, I'm going to give this 'Eiffel Tower' and 'I Love Paris' here manually. And then, I'm going to zoom in a little bit more so I can give another annotation and it won't matter because every time I add a new annotation, the coordinates will get changed. So, whatever I did before won't matter when I add a new annotation, this will change the chosenLatitude and chosenLongitude. So, I will be saving the latest coordinates that I have chosen in this app, okay? So, when you come over here, as you might remember, chosenLatitude and chosenLongitude gets new values every time I click on this. So, right now, I have my name, I have my comment, and I have my annotation. So, I have my coordinates. And, as you can see when I hit 'Save', it gets saved on the Core Data. So, in order to be sure, of course, we have to implement a fetch request and get the information on display to the user in a TableView. So, let's stop here and we're going to do those things in the following lectures.

 

About the Author
Students
414
Courses
55
Learning Paths
3

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.

Covered Topics