URLSession ile API Çağrıları
async/await kullanarak veri alın.
URLSession ile API Çağrıları, CoddyKit'te ücretsiz bir SwiftUI Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, SwiftUI Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. SwiftUI Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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. 🚀
Sıkça Sorulan Sorular
“URLSession ile API Çağrıları” dersi ücretsiz mi?
Evet — “URLSession ile API Çağrıları” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve SwiftUI Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. SwiftUI Academy kursu toplamda 4 dersten oluşur.
“URLSession ile API Çağrıları” dersinde ne öğreneceğim?
async/await kullanarak veri alın. SwiftUI Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
SwiftUI Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te SwiftUI Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“URLSession ile API Çağrıları” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu SwiftUI Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her SwiftUI Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Codable ile JSON Çözümleme
- URLSession ile API Çağrıları
- .task Değiştiricisi
- Yükleme ve Hata Durumları