Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Get Data from a Session

Learn to work with URLSession and how to use it to download data from a server.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Welcome back! The URLSession API has many moving part, centered on the URLSession class itself.

import SwiftUI
let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration)
guard let url = URL(string: "https://itunes.apple.com/search?media=music&entity=song&term=starlight") else {
  fatalError("Could not create the URL.")
}
let (data, response) = try await session.data(from: url)
Task {
  let (data, response) = try await session.data(from: url)
}
guard let httpResponse = response as? HTTPURLResponse,
      (200..<300).contains(httpResponse.statusCode)
else {
  return
}
guard let httpResponse = response as? HTTPURLResponse,
      (200..<300).contains(httpResponse.statusCode),
      let dataString = String(data: data, encoding: .utf8)
else {
  return
}
print(dataString)

Reviews

Comments