Menu Close

Why do we need to use SwiftUI and combine?

Why do we need to use SwiftUI and combine?

To help us reason about SwiftUI and Combine, we’re going to use a simple sign-up screen which lets users enter a username and password to create a new account in an application. In a later article, we will add a login screen to demonstrate some of the additional benefits of using Combine.

How to do an HTTP request in Swift?

Doing an HTTP request in Swift is pretty easy, you can use the built-in shared URLSession with a simple data task, and voilá there’s your response. Of course you might want to check for valid status code and if everything is fine, you can parse your response JSON by using the JSONDecoder object from Foundation.

How to get a JSON response in Swift?

Convert JSON HTTP Response to a Swift struct. In this tutorial, I will use public and free at the time of writing this tutorial RESTful Web Service: https://jsonplaceholder.typicode.com. This web service endpoint allows me to send HTTP Requests to it and get back JSON response.

Can you use a shared urlsession in Swift?

Nothing special so far, just some basic Codable elements, and a simple error, because hell yeah, we want to show some error if something fails. ❌ Doing an HTTP request in Swift is pretty easy, you can use the built-in shared URLSession with a simple data task, and voilá there’s your response.

Convert JSON HTTP Response to a Swift struct. In this tutorial, I will use public and free at the time of writing this tutorial RESTful Web Service: https://jsonplaceholder.typicode.com. This web service endpoint allows me to send HTTP Requests to it and get back JSON response.

How to send and receive data in SwiftUI?

Even better, when the request completes we can immediately assign its data to properties in SwiftUI views, causing our user interface to update. To demonstrate this we can load some example music JSON data from Apple’s iTunes API, and show it all in a SwiftUI List.

How to send an HTTP GET request in Swift?

For example, if I open the following URL in the browser windows: https://jsonplaceholder.typicode.com/todos/1 I will get back the following JSON document: Below is a very simple example of how you can send an HTTP GET Request in Swift.

Why was the urlsession API made before Swift?

This strange state of affairs exists because the URLSession API was made before Swift came along, so there was no nicer way of representing this either-or state. Notice the way we call resume () on the task straight away?