0Pricing
SwiftUI Academy · Lektion

APIs mit URLSession aufrufen

Rufen Sie Daten mit async/await ab.

APIs mit URLSession aufrufen ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What URLSession Does

URLSession is Apple framework for talking to the network. You give it a URL and it fetches the response for you. 🌐

Building a URL

Start with a valid URL. The initializer is optional because text can be malformed, so unwrap it before using.

let url = URL(string: "https://api.example.com/users")!

The async Fetch Call

The modern way to fetch is async/await. You await data from a URL and your code reads top to bottom, like normal.

let (data, response) = try await
    URLSession.shared.data(from: url)

You Get Data and Response

The call returns two things: the raw data bytes and a response describing status. You usually decode the data next.

Combining Fetch and Decode

Fetch then decode in two short steps. await the bytes, then hand them to JSONDecoder to get a typed value.

let (data, _) = try await URLSession.shared.data(from: url)
let users = try JSONDecoder().decode([User].self, from: data)

Async Functions

Wrap network work in an async function. Marking it async lets you use await inside and keeps the UI responsive.

func loadUsers() async throws -> [User] { }

Why throws Matters

Networking can fail, so fetch functions are throws. Callers must try, which forces you to think about errors.

Checking the Status Code

Cast the response and read its statusCode. A value of 200 means success, while 404 or 500 signal problems.

let http = response as? HTTPURLResponse
if http?.statusCode == 200 { }

Reusing the Shared Session

For most apps, URLSession.shared is all you need. It is a ready-made session with sensible default settings.

Custom Requests

For headers or POST bodies, build a URLRequest. Set the method and fields, then pass the request to the session.

var request = URLRequest(url: url)
request.httpMethod = "GET"

Keep Networking Off the UI

Always do fetching in an async context, never on the main thread directly, so taps and scrolling stay smooth.

Quick Check

Quick check on fetching data from a URL.

Recap: URLSession

You can now await data from a URL with URLSession, check the status code, and decode the bytes inside an async throwing function. 🚀

Häufig gestellte Fragen

Ist die Lektion „APIs mit URLSession aufrufen“ kostenlos?

Ja — der vollständige Text von „APIs mit URLSession aufrufen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „APIs mit URLSession aufrufen“?

Rufen Sie Daten mit async/await ab. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um SwiftUI Academy zu starten?

Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „APIs mit URLSession aufrufen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?

Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. JSON mit Codable dekodieren
  2. APIs mit URLSession aufrufen
  3. Der .task-Modifikator
  4. Lade- und Fehlerzustände
← Zurück zu SwiftUI Academy