Beginning Networking with URLSession

Sep 13 2022 · Swift 5.6, iOS 15, Xcode 13.4.1

Part 2: Download Data

13. Challenge: Download Images

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 12. Handle Errors Next episode: 14. Show Download Progress

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Hello and welcome! Time for a little challenge for you. By now you should have a good idea about how to download a file using URLSession.

  let (downloadURL, response) = try await session.download(from: url)
guard let httpResponse = response as? HTTPURLResponse,
    httpResponse.statusCode == 200
else {
  throw ArtworkDownloadError.invalidResponse
}
do {
  return try Data(contentsOf: downloadURL)
} catch {
  throw ArtworkDownloadError.failedToDownloadArtwork
}
do {
  let data = try await downloader.downloadArtwork(at: artworkURL)
} catch {
  print(error)
}
guard let image = UIImage(data: data) else {
  return
}
artworkImage = image
catch {
}