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

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've reached locked video content where the transcript will be shown as obfuscated text.

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. Your challenge is to take the current app and have it download some album art. I have added a method to Song Downloader called Download Artwork. I want you to use the artwork property of music item in order to create the URL from where to download the album artwork image. Remember, when you download an image it'll be downloaded as a data object. You'll need to convert that to an image and then assign that image to the artwork image property of song detail view. Make sure you remember the golden rule about having all UI updates happen on the main thread. Pause the video, give it a try, and I'll see you in a bit. (contemplative electronic music) Welcome back. How did it go? Let's go over how you could have solved the challenge. Open SongDownloader.swift, and add this code where the to do was. Same pattern where you use URLSession to download a file from the network. Next, check the response to status code to ensure the request was successful. If the status code is not 200, you throw an invalid response error. Finally, add this code at the end of the method. Because the downloaded file is an image, use the contents of static method on data in order to create a data object from the file and return its contents. The call can throw errors, So you wrap all of this inside a do catch block, and throw an error should something go wrong. Switch over to SongDetailView.swift, and add this code where the to do was. This code, wrapped inside a do catch block, calls the download artwork method and stores its return data. At the end of the do block, add this code. This tries to create a UI image from the data that should contain the downloaded album artwork. Finally, update the artwork image property. Time to build and run your code. As soon as you launch the app, your code to download the artwork should run. Depending on the speed of your connection, you should see the downloaded album artwork replace the default image. Of note, the error that can get thrown and is caught in the catch statement is currently just being handled via a print statement. Once you're done doing any tests, be sure to remove it. Fantastic work. Another successful challenge for you. In the next episode, we'll take a look at how to show the progress of your download. See you there.