iOS 15: Sending and receiving Codable data with URLSession and SwiftUI – Cupcake Corner Tutorial 2/9
Why Stack Overflow recommends it
Stack Overflow users link it as the guide beginners follow for loading and decoding JSON from a URL in SwiftUI, and often cite it when their copied code fails to display results.
Recommended by 2 people across 2 different months, 2022 to 2022.
What it is
Part 2 of Paul Hudson's Cupcake Corner project in his 100 Days of SwiftUI course. It shows how to fetch JSON from a web API with URLSession, decode it into Codable structs, and display the results in a SwiftUI List using async/await and the task() modifier.
What people said
Recommends David Zatica · 2022-10 · Stack Overflow · on “IOS app to display values via BLE from an ESP32”
Have you tried setting the ESP32 up as a web server and creating an API based on the values? Using this instead of bluetooth allows you to view the values from anywhere with internet connection, and is easier to implement and use across different platforms You can do this by implementing code into the ESP32, so that when you make http requests, it returns with a value, just like any other API would. Then in Xcode where you are building your IOS app you can make http requests to the web server and retrieve values for example, in the ESP32 you can do something like this (assuming you are using the Arduino IDE to program it): server.on("/value", [](AsyncWebServerRequest * request)) { String val
Recommends brando · 2022-11 · Stack Overflow · on “Difficulty calling an API for quote - unable to display results in list format (Beginner)”
My Code: import SwiftUI struct Response: Codable { var results: [Result] } struct Result: Codable { var author: String var text: String } struct ContentView: View { @State private var results = [Result]() var body: some View { List(results, id: \.author) { item in // <--- This is where the problem is VStack(alignment: .leading) { Text(item.text) Text(item.author) } } .task{ await loadData() } } func loadData() async{ guard let url = URL(string: " [LINK] ") else { print("Invalid URL") return } do { let (data, _) = try await URLSession.shared.data(from: url) if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) { results = decodedResponse.results print(results) } } catc
Cites Cat needs logic · 2022-01 · Stack Overflow · on “Trying to learn how to pull data from urls, followed guide completely, still wrong”
This is the guide I'm trying to follow: [this video] This is my code (Exact same as his): import SwiftUI struct Response: Codable { var results: [Result] } struct Result: Codable { var trackId: Int var trackName: String var collectionName: String } struct ContentView: View { @State private var results = [Result]() var body: some View { List(results, id: \.trackId) { item in VStack(alignment: .leading) { Text(item.trackName) .font(.headline) Text(item.collectionName) } }.task { await loadData() } } func loadData() async { guard let url = URL(string: " [LINK] ") else { print("Invalid URL") return } do { let (data, _) = try await URLSession.shared.data(from: url) if let decodedResponse = try? JSONDe