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 start writing our codes so that we can get the data to display to the user. And let me write down our steps that we have talked about. We're going to learn a lot of new things in this section so that in order not to get confused, we're going to follow these steps. First, we're going to create a request and we're going to open a session, which means we're going to go to that URL that we have talked about, okay? So, we're going to go to this URL. And, after going to this URL, we're going to get the data inside of that URL, right?
So, this API will provide us with data and we're going to get this response. After getting this response, after getting this data, we're going to have to process it. It's called Parsing or JSON Serialization, okay? So, this is spelled like this 'JSON Serialization'. And it means that processing the data in order to show users that data that we want. For example, we don't want anything besides the currencies that we have chosen, right? So, what are we going to do? First, in order to create this request and open a session,
we're going to create a 'url'. So, we have our URL, right? And we have this URL, class URL type, in Swift. So, if you write 'URL' and open a parenthesis, it will ask you for some parameters. And as you can see, we have a lot of options in here. I'm looking for a simple string. So, as you can see, this is what I'm looking for because I already have my URL in a text format. So, if you come over your browser and if you copy this from your browser, let me 'Copy' this. And then, you're going to have to come to your Xcode and paste this as a string. So, that's it. That's how you create a URL.
So, make sure you put the string inside of quotation marks, of course. And then, later on, you're going to have to create a 'session'. So, in order to create a session, we're going to use this URLSession class. So, as you can see, this gets the data and manages transferring tasks from data in specified network. And that's exactly what we want. If you say, 'URLSession.shared', this creates an instance of this URLSession class. So, this creates a singleton session, which means that you get the same instance when you create this class so that you won't get confused when you try to create more than one. Actually, multiple requests, okay?
We're going to be working with this only instance when we try to create sessions. And after creating this object, after creating this instance, actually, we're going to start this task. We're going to start this session by saying, '.dataTask'. And it will ask us for a URL. But, I'm not going to do this on the same line, okay? Rather, I'm going to create a variable called 'task' and you're going to see why I'm doing this, okay? And after you create this task, you can derive this from the session variable that you have created, obviously. Then, you can come over here and say, 'session.dataTask'. It will ask you for a URL. And, we want the one with completionHandler. So, what is this completionHandler? It actually defines what's going to happen once we start this task. So, we're going to give an input in here; input being the URL that we have created, okay? And it will give us an output, it will give me a data and a response or an error, okay? So, I will keep track what's going on so that if I get an error, like if I cannot reach the URL, I can display it to the user. If I get a data, I can just process it and display it to the user again. So, this completionHandler, actually, is very good for me to implement.
So, make sure you choose the one with the completionHandler and we're going to use this a lot in the rest of the sections as well. Because after this section, you're going to be dealing with this kind of situation constantly. So, in the URL, we can just force unwrap this. And if you don't do this, as you can see, it gives us an error because I'm certain right now that this is going to work. And, of course, you can use 'if let' if you want. If you hit 'Enter' on the completion block, it will create a closure for you.
So, as you can see, these are the outputs of our callback function, our completion block. And, in this block, we can write whatever we want. We can define what's going to happen after this task got executed. So, this structure is called closure. So, I'm going to give this some variable names, like data, response, and error. You can choose your own variable name as well. And, for example, I'm going to say, 'if error != nil', and I'm going to display an alert message, 'if the error != nil', in this case. And again, this structure is called closure. And we're going to be using that from now on again.
So, let me create my alert in here. Remember, we use 'UIAlertController' in order to achieve this. And it will ask us for a title, and a message, and a style. And, for a title, I'm going to go for 'Error'. And, the message will not be something constant. I'm going to use this 'error' message that comes from the output of this task. And if you say, '.localizedDescription', it will just create a description that user can understand, like you have no connection or you can just try again later.
If you say, '.' here, you can just go for 'alert'. And remember, we're going to have to create a button in order to close this down. And for a button, I'm going to use the default style and I don't need any handler in this button. And I'm going to add this button as an action to my alert. And, for the last time, I'm going to present this. But since we are in a closure, we have to say, 'self.present', okay? So, 'self.present', I'm going to present the alert. Of course, I want the animation as usual and the 'completion: nil'. And 'else' if you don't have any error, it means that I may have a data, I don't have any error, okay? And, I believe, I misspelled true. And since I don't have any error, I can just check to see if data is nil or not. And 'if data != nil', I can go ahead and process it to show to the user, okay? And, in fact, in this... Actually, in this part, I completed the first step. So, 'if data != nil', it means that I managed to get... I managed to create the request. And after this, I will get the response, I will get the data. And then, later on, I'm going to parse it. I'm going to process it to show to the user, okay? So, this is number one, this is number two. So, that's it for right now, we're going to stop here. And within the next lecture, we're going to continue building our Currency Converter app.
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.