Download a webpage

 Problem

I want to download the source (HTML) of a specific web page, given its complete URL.



 Recipe


// Set the page URL we want to download
let URL = NSURL(string: "https://iswift.org")

// Try downloading it
do {
    let htmlSource = try String(contentsOfURL: URL!, encoding: NSUTF8StringEncoding)
    print(htmlSource)
}
catch let error as NSError {
    print("Ooops! Something went wrong: \(error)")
}
				


 Playground

Find answers here

Hi there! Ask Us a Question We are ready to help