APIs and JSON
The course is part of this learning path
In this course, we're going to build a currency converter app in order to teach you about JSON and APIs. We're going to get some data from the internet, process them, and then display them to the user.
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 actually get the data to display to the user later on or we can just print if in our log so that we can be sure that we are getting the data. And in order to make this work, we have to do two more things in order to ensure we are creating this request and getting the response. As you can see we have a warning in here saying that you have created this task but you have never used it. Remember in the previous lecture I've told you that I'm doing this task, I'm assigning this to a variable for a reason and you're going to see why. Because we could have easily done this in the same line as well. We could have just said URLSession.shared.datatask but we want to resume this task after we create it. So, we have to say task.resume. So, this doesn't make sense at least from a grammar perspective, we're not resuming this, we are starting this in fact but we have to use this method in order to start this. So, once you create the task, you can just say task.resume in order to fire this up. So, this will start our task up, but we have to do one more thing. Because we're working with an http rather than https. So, we're trying to reach an http website, and if you come over here you can see it displays this website is not secure because we're not using http secure, https. For example, let me go to my own website which is this website. And in here as you can see this display s a lock icon indicating that this is encrypted with an SSL certificate. It means that everything that is coming or going out of this website server is encrypted with an SSL certificate. So, it's safer to use https because all data transfer will be actually encrypted. So, if you have taken my complete ethical hacking course, you would have remembered that there is an attack called man-in-the-middle attack that are done by hackers in order to steal your data when you're on the same network. For example, suppose that you're connected to a router and some hacker is actually connected to that router as well, that hacker can easily be a man in the middle. It means that they can actually monitor, they can actually track the packets that you're getting and giving to the web servers. And if these packets are actually encrypted, they cannot see the content. So, even though they know you're going to that website, they cannot see the content of that packets. But in this case you're using an http and this is not secure as https. So, if this was an https then it wouldn't have matter. But in this case we're going to have to go to info.plist and we're going to have to find something called App Transport Security. It can be on the upper side or the downside when you first open this drop down menu. So, find this App Transport Security Settings and then click somewhere outside of this info.plist. Then you're going to have to click on this little arrow to make it face downwards. Once you do that you can come over here and click on the plus sign and it opens a new menu under the App Transport Security Settings. And in the settings you're going to have to find some menu called Allow Arbitrary Loads. And this has a default value of no, make it yes. Once you do that then you can create request to the http websites rather than https. So, in any case if you're working with an http connection, you're going to have to do this. You're going to have to make your App Transport Security Settings modified. So, once we do that now we are ready to get the data and try to print it out so that we can be certain that we're getting the data. And in here if data is not nil it means that we actually have some data. For right now we're going to use something called JSONSerialization. Remember it's called JSONSerialization, and this is a class that converts between JSON and the foundation objects. So, we're going to be converting our JSON format into strings into Boolean into integers or doubles. So, this is how it goes. You're going to have to say .jsonObject and it will ask you for a data, and of course this is the data, and it will ask you for some options. And as you can see this trojan error, which means that we're going to have to do this with try keyword. So, this is obviously the data that we are looking for then I believe we can just force and wrap this because we have already checked if this is nil or not. And if you hit 'Enter' over this ReadingOptions and hit that, it will give you some options. And in here we're looking for the mutable containers. So, the containers actually allow us to work with arrays and dictionaries. Remember the type that we're getting in our JSON data kind of looks like dictionary. So, it has some key value pairings that we have seen before. So, we don't have any immutable containers. So, mutable means it can be changed, immutable means it cannot be changed. We're not interested in changing anything but we are interested in the container so that we can work with dictionaries. So, we're going to choose the mutable containers. After that, of course this gives an error because we're going to have to work with try and in fact I'm going to assign this to a variable and I'm going to try to make this happen. So, let me say something like let jsonResult or jsonResponse. And I'm going to try this and assign this to my jsonResponse. And let me do this over. Do try catch. So, do try catch, and if we catch any error we can just print out some error. And in here after trying to convert this to be a JSON object, let me print error over here before we forget, we can just get the data and print it to the user in a JSON response format. So, after that what I'm going to do, I'm going to print this out to see if we get the result or not. If we can get the data then we can go on and parse it, process it to get the single values like USD, Turkish lira and other stuff. And before we do that of course, we have to be certain that this is working so far because we have created a lot of codes in here. And the way that I'm going to do this to be a some kind of a synchronized operation like we did in the face recognition lecture. So, we are doing all of this in background, meaning that it won't affect the user interface. It won't block the current process that is going on in my app. So, that user can actually interact with the user interface, user can actually see what's going on in the app. They can click on the buttons, they can play with the app whatever they want, and it won't block, it won't interfere with the current processes. On the other hand, after this operation, I'm going to change the user interface as well. I'm going to change the labels. So, as I have done in the face recognition lecture, I'm going to use this asynchronously with using DispatchQueue. So, if you haven't watched the face recognition app, I suggest you go back and see what's going on in that lecture. So, synchronous means wait for every process to be finished and then move on to the next one. Asynchronous means it doesn't have to be synchronized so we can do two things at once and we're going to see what's going to happen after the other process is finished as well. And it won't affect the way that we are using the app. So, it won't block the user interface. So, remember we did that in the candy section, in the countdown timer section as well. So, I'm going to go for DispatchQueue and I'm going to say this main, okay? So, I'm going to be working with the main track but asynchronously. So, this will create me a coding block and I'm going to print out the data or jsonResponse that we have got from the website and we're going to use this response. And I believe now we are ready to test this. We have been writing so much code and we got to see if this is working or not. So, let me run this in our simulator and if this works out we're going to be seeing the same result that we have seen in the browser but this time we're going to see that result in our logs. So, make sure you print this jsonResponse out and try to be ready for what you're going to see in your logs because this is the first time that you're interacting with the Internet in this IOS development course. So, this is the moment of truth for us. Once the simulator opens, click on the 'Get Rates' button and here you go now we have the data in our logs and this is exactly the same thing that we have seen before in our browser. So, if you come back to your browser you can just compare this data. As you can see we have the rates, we have every other data that we have seen like the USD, the Turkish lira, the Japanese yen and we have the time stamp, the success and everything. Now, let's stop here and within the next section we're going to see how we can actually parse this data.
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.