Search code examples
iosxcodeswiftnsurlnsurlsession

Downloading HTML Content


I'm trying to get the html of a web page with this code:

let url = NSURL(string: "http://www.handasaim.co.il/news.asp")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
    (data, response, error) in
    if error == nil {
        var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)
        println(urlContent)
     }
}

 task.resume()

I think the problem is that the url ending is .asp because when I tried it with a different url it worked.

What's the problem?


Solution

  • The webpage you've specified does not use UTF8 encoding. If you'll specify the correct encoding when trying to create NSString from data - it will work.

    var urlContent = NSString(data: data, encoding: NSASCIIStringEncoding)