2

I need to get the data from the url it works for other url like udemy but returns nil for url="http://www.google.com/finance/converter?a=1&from=USD&to=INR" how to solve this issue. i have added app transport security too..

screen

let url = NSURL(string: "https://www.udemy.com/the-complete-ios-9-developer-course/")!

   let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
    if let url_content = data{
        let webcontent = NSString(data: url_content, encoding: NSUTF8StringEncoding)
        print(webcontent)
    }
    }
    task.resume()
Aravindh Kumar
  • 1,213
  • 11
  • 22

2 Answers2

0

You can check the "error" variable to see if you have an error returned by the call. Most probably you have an error accessing that url.

Note that I tried the code and I could get data.

Gabi D
  • 199
  • 1
  • 8
0

This is a text encoding problem. The webpage at http://www.google.com/finance/converter?a=1&from=USD&to=INR is not encoded with UTF-8 but with ISO-8859-1.

In this case, you have to use NSISOLatin1StringEncoding instead of NSUTF8StringEncoding for NSString:

let webcontent = NSString(data: url_content, encoding: NSISOLatin1StringEncoding)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • I used `curl -I theURL` in the Terminal to check the response headers. But now I just remembered that Martin R already gave an answer about how to get the same info via Swift: http://stackoverflow.com/a/32051684/2227743 – Eric Aya Jan 04 '16 at 10:04