Swift:
Downloading a web page

How to:

Let’s use URLSession to do the job. Swift makes it straight to the point.

import Foundation

let url = URL(string: "https://www.example.com")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
    if let error = error {
        print("Error:", error)
        return
    }

    if let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) {
        if let mimeType = httpResponse.mimeType, mimeType == "text/html",
           let data = data, let string = String(data: data, encoding: .utf8) {
            print("Downloaded web page content:")
            print(string)
        } else {
            print("Invalid MIME type or encoding.")
        }
    } else {
        print("Server responded with error.")
    }
}
task.resume()
// Make sure the playground keeps running until the task completes
RunLoop.current.run()

Sample output might look like this:

Downloaded web page content:
<!doctype html>...

Deep Dive

The URLSession API has been around since iOS 7 and macOS 10.9. It was a game-changer back then, replacing the older, more cumbersome NSURLConnection. While URLSession is powerful and flexible, you could also consider third-party libraries like Alamofire for more complex networking needs.

When implementing, remember that network requests are asynchronous. This means your app can carry on with other tasks while the server gets back to you. Also, using URLSession properly involves handling errors gracefully and checking the server’s response status. The MIME type checking is crucial to ensure you’re receiving HTML, not other file types like JSON or an image.

See Also

Dive deeper or explore alternatives: