This week in iOS Development(API’s and JSON)

This week in iOS development, I was able to make a weather app that requested and stored your current location and then gave you the current weather for the next five hours and the next five days.

I had a lot of fun with this app because for the first time I didn’t have to work with data that was local on my computer or that was already provided for me. I was responsible for finding out how to get the right data I needed, as well as retrieving and decoding the data to present it to the user.

What is an API?

API stands for Application Programming Interface, a way in which to get data from someone providing it. Think of it as an agreement between you and the company that you want data from. If you want data from them, you have to ask or call for it the way that they want you to, and in exchange, they promise to give you the data you’re asking for and not change it on you later.

This is pre-written code by them so that they can provide you with data for use. In this project, I used the free API’s from openweather.org to get all sorts of data. They provided anything from a forecast per minute, per hour, per day, and even things like government weather warnings. But for this project, I just needed the current weather of my location and then an hourly forecast for five hours and a daily forecast for five days.

To ask for data, you have to call the API like this.

This is what one of their API’s look like. I would enter this into the browser but I would also have to input some info. The info would be done from the app. When I get the current location of the device, I can work with a longitude and a latitude. I would then plug those into the orange {lat} and {lon} in the API call. I would then add an API key at {API key} which is provided by openweather.org to me when I make a free account. I would put this into the web browser and it would return to the data I needed. Now the next step.

What is JSON?

It stands for JavaScript Object Notation. Why, does this matter? Because this the language that the data comes back in. Data used to be given in XML format but now JSON is much more popular. The JSON data that comes back isn't the most legible thing to work with, so what I did is downloaded a web browser extension that allows me to see this JSON data in a better, more understandable format. I use JSON Viewer pro.

without JSON viewer
with JSON viewer

Now the problem with using JSON in my weather app is that I use the Swift language. So in order to use this data I need to decode and parse it into a structured and useable way. Luckily apple gives us the JSONDecoder and Decodable protocols to handle this efficiently.

One of my take aways from this week was from solving a problem. The problem was I would first call a function like requestLocation(), which would lead to everything from grabbing the location, storing the location, making a url, making the API call, receiving data, decoding it, and then parsing it. After that function was called I would do another function called reloadData() on my collection view which shows all the data. The problem was that the time it took to request location and get all the data over the internet, it still wasn't parsing it in time before the reloadData() function was called. So the data wouldn't show.

What I ended up doing was creating a protocol that tracks when the data is finally parsed in my app. I then set the delegate to the ViewController(which displays the data) to trigger reloadData() once the data was parsed. Problem solved. I learned that functions dont wait for other functions to be done unless programmed too. This is key when working with data over the internet because there are many factors like internet speeds and other things on both client and server sides.

This article was a brief summary of my week and the things I learned. I didn't want to delve to deep this time but there is so much to grasp this week. I’m really excited where I’m at in app development because I know a lot of the work of iOS Developers is working with API’s and parsing JSON. So hopefully once get this down I can start looking for jobs. Well see.

I’m a father of five and an iOS Engineer.