API calling in Swift

Updated 21 December 2023

Save

Almost every app needs to fetch data from the backend API or ‘server’. So, we will look at a basic approach to a network call in Swift.

Let’s check how can we implement API calling in swift. We will not be using any third parties or network calls instead, we will be using Apple’s own API services URLSession.

Things need to check before integrating API into the app :
URLSession
Completion Handlers
DispatchQueue

URLSession:

The URLSession is used to create URLSessionTalk instances, which can fetch and return data to your app, and download and upload files to web services.

Completion Handlers:

A completion handler in Swift is a function that calls back when a task completes.

DispatchQueue:

An object that manages the execution of tasks serially or concurrently on your app’s main thread or on a background thread.

Now let’s start working on API calling.

I am using below mentioned API you can check its data by hitting it in a browser.
https://swapi.dev/api/films/

Now based on the data we are getting from API create a Data Model.

Making a Network Call in Swift:

Finally call this function in viewDidLoad & show data on your screen accordingly.

FULL CODE:

OUTPUT:

I have shown different film titles in a Tableview.

Conclusion:

In this article, I have explained API Calling in swift.

Thanks for reading this article ❤

If I got something wrong 🙈, let me know in the comments. I would love to improve.

For more interesting blogs check out here – https://mobikul.com/blog/

Reference link: https://www.freecodecamp.org/news/how-to-make-your-first-api-call-in-swift/

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home