How to download files with URLSession and downloadTask()

URLSession is designed to make network transfers as easy as possible, and a great example of that is its downloadTask()` method. This fetches the contents of a URL you specify, saves it to a local file, then calls a completion handler so you can manipulate the file – all in one.

To demonstrate this, here’s some code to download the source code to the apple.com homepage: