XCode API Usage [ Fetch Data from API ] - SESSION IV

 

In this XCODE learning lab [Session IV] we are gonna look at how we can communicate with websites to fetch some data in the process of GET Requests. Data extraction will happen from Website to be displayed at the App.

 
 
 
Video file

 

In the last XCODE learning lab [Session III], we learnt how we can communicate with Apple’s CloudKit database. In this XCODE learning lab [Session IV] and the next one XCODE learning lag [Session V], we’re gonna look at how we can communicate with certain website’s databases, fetching and posting data. This learning lab will be focused on fetching data which is called GET Requests. I will take you to the best method to fetch data and store it in custom data model in our App.

 

Image removed.

 

Let's share briefly, what is an API or an Application Programming Interface. “These API’s let your product or service communicate with other products and services. An API simply states the rules for communicating with the website database.” A few examples are weather apps fetching data for the weather from different sources. Login flows also POST the credentials into API’s and get a response indicating if the credentials are valid or not.

 

The apps and services that communicate to an external service can’t actually access their database but can only talk to the API layer of the website where you pass in what you want, according to the rules stated by the API, and you get a response back.

 

This API functionality is the most critical aspect in your App development due to its significance to connect your app to external as well as internal databases.

 

While connecting with the API, you might have to authenticate yourself by providing an api_key in the url, though this is not mandatory. This process will provide an additional level of security but even if the api doesn’t have keys you can still proceed onto the next step. You will get a data response back from the API in case of success or an error message in case of failure with details on why the error occurred.

 

Now you might think it's hard communicating with API’s because each one has a different set of rules that you have to follow. Most of the organisation maintain manuals or documentation which covers set of guidelines and rules. But some websites don’t have an API layer so we can’t talk to them. Now, no more talking, let me show you what we’ll be building.

 

As everyone of us have used News paper or read article or documents I have taken News App for ease of understanding.

As you can see, this is a news app where you can get news articles from an api endpoint, and then display it on your screen which covers Web images and headlines.

 

In our app, we’re gonna be using:

 

  • A list to display the news items.
  • Functions with code to fetch from the given API’s.
  • Async Images, to show a web image.
  • Text Views, to display the title and description

 

Now let’s go through what we learnt today.

 

Image removed.

 

  1. API’s and how we can use them
  2. The process of fetching from API’s.
    1. Fetching the data in JSON Format.
    2. Creating a model to hold the data.
    3. Using JsonDecoder() to decode the data.
    4. Saving it in a state variable
  3. Using a list to display the information
  4. Async Images to display web images
  5. Text Views to display the headline

 

Thank you for watching, in my next learning lab I will come back with how to use POST requests to post data to an API. I am sure it will be a great and enriching learning experience for you. Let's innovate, challenge & broaden our intellectual horizons, and make it an interesting and joyous learning experience.

 

Till then, let's enjoy every moment! 🙂 🙂