Basic API Requests with Swift 5A tutorial on retrieving and displaying images in your app using API requestsFelipe ValdiviaBlockedUnblockFollowFollowingFeb 10, 2018Hello everyone, today’s piece is a basic tutorial on how to perform API requests using Swift 5.
I am currently a freelance iOS Developer who finished the iOS Development nano-degree from Udacity and am writing this for educational purposes.
Let’s start by creating a new single view project in Xcode:Xcode new projectI’ve named this project playingWithRequests.
Let’s take a look at our goals with this project:Learn how to make a request to an API using Swift 5.
Learn how to use URL and URLSession.
Learn how request works in Swift.
Show an image from the internet.
Now that we have our project created, we are going to drop an image to our storyboard.
Expand the image to cover the entire view and change the content mode to aspect fit like this:Setting up our imageWith our image in our storyboard, we can create our IBOutlet in order to manage the image in our Swift code:Image IBOutletNow we have everything ready to start our project.
For this example, I am going to use an image of Saturn that I found on Google.
All of our code is located inside the viewDidLoad() function.
The first class that we are going to use is called URL, which lets us represent an URL.
Create an instance of this class and paste the string of our Saturn URL:An instance of URL class (Swift 5)Now that we have our URL, we are going to ask for the data.
The URLSession class can manage network requests, let’s use it to make a request to this network.
We can create a URLSession with our own setting, but for this example, we are going to use the class call share that contains the default settings for us to do a simple GET request.
Before we begin coding, I want to cover one more thing.
When we use the class URLSession, a network request is called a task.
This is important to remember because we are going to be working with tasks (requests) every time we need to interact with an API.
Udacity Material showing properties of URLSessionWe are ready to create our task to the URL, we are going to use the class call dataTask:Data taskWe are using URLSession to start a request to the URL that we stored in our constant imageURL.
With this method, we have a completion handler which is going to help us to manipulate our data in case we get an error, checking first if our error variable is nil.
We can now show the image in our view:code to show an image from a network requestSwift 5 update: so far all the code is the same for Swift 4, but before continuing I want to mention that with the update to Swift 5, the library now includes something call Result Type.
Result Type works just like an Optional Type but in this case, the result type is implemented as an enum.
This works really well with asynchronous tasks because before even getting the data we ask for success or failure.
We are not gonna see this here, I will write a specific tutorial for this topic in the future.
If we run our project right now you are not going to see anything in your app.
We have the code for the request, but we need to run our task using the method call resume():Project finishedNow if we run our app you are going to see a blank page.
Press the cmd + shift + h to simulate a click of the home button and open the app again.
The image will appear:Completed appI’ll cover why we need to press the home button and open again the app in my next piece but basically, it is due to our function running in the background of the app and needing to be brought to the foreground.
I hope you guys liked this piece.
It’s pretty basic but if you have any questions please leave a comment or send me an email, I am going to continue writing about different Swift topics, so follow me and let me know if there is any specific topic that you wanna learn about.
Regards!.. More details