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: "http://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)")
}
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground