Learning how API work in iOS and how to extract JSON

N!TESH.
N!TESH.
Feb 27, 2017 · 4 min read

What is an API’s ?

API stands for Application Programming Interface. It is a programmatic way to be able to access functions and data of other apps. Today pretty much every major platform has an API, most of them are free and you can access them to work with maps and location you can use facebook, twitter or google and all the data they have. So API’s allows you to access a huge amount of data and built apps that rely on existing systems like facebook or twitter.

Why do we use API’s ?

Do you use an application that tells you the location of your favorite restaurant? Or how about an app that shows when the next bus will be at your stop? Most tools like these rely on open APIs to run and pull the most accurate data. Those are both good examples of how open APIs might help you in your everyday life.

How do we use API’s ?

To demonstrate how this all works in iOS with Swift we are going to use the API of open whether map to get the weather of a particular location. So let’s get started go to openwhethermap.org and create an account. Once you have created an account the main important thing you are going to need is an API key which you will find on the website itself, copy it somewhere since we are going to be using it very shortly.

Now if you jump over to the API tab on the website you will find a bunch of API’s there, we are going to focus on the Current whether data API. Now if you click on the API Docs you will find the required API on it. But when you click on one of the API’s you will get 401 error, that is because you will have to provide your own API key instead of the existing one in the URL if you notice at the end.

http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1

Change it with your own and you have your data in the form of JSON which looks like this

This might not look very friendly but it is very powerful. JSON stands for Java Script Object Notation. It’s designed to simply store data in plane text format, and almost every programming language provides functions including Swift to process this data. Now let work towards it.

Create a new Swift project. Everything we are going to do will be in ViewController.swift. Let’s start by creating an URL with the one we have been provided by open whether map with your API key at the end.

If you notice the URL is HTTP and not HTTPs so we need to edit our info.plist to allow that domain name. There are various ways to do it so let us open the info.plist file as source code and add the code below.

Now let’s go the ViewController and code. We will create an URL, create a task from the URL to go and get the content of that URL. Checking for an error and if there isn’t one then checking if we can work with the data with the help of JSONSerialization object, and we can then process the data so that we can work with the data in a usual swift manner. So let’s see how that looks.

jsonResult is the array in which you will have your data which will be printed in the console if you check. this is the same data you will use to display on your apps.

This is just a basic working of how an API works in Swift. With API’s, developers can quickly and inexpensively launch useful tools to benefit both the business and their customers. With an increasing number of businesses creating and adopting API’s into their systems, the best is yet to come.

Aviabird

Aviabird Technologies provides enterprise software consulting in Angular, Rails, Elixir, Golang, React, Flutter and other technologies.

N!TESH.

Written by

N!TESH.

a “work in progress”

Aviabird

Aviabird

Aviabird Technologies provides enterprise software consulting in Angular, Rails, Elixir, Golang, React, Flutter and other technologies.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade