2

I have an app that downloads the html of a website the contains characters in hebrew, this is the code:

let newurl = NSURL(string: "http://www.handasaim.co.il")
var error: NSError?
let html = NSString(contentsOfURL: newurl!, encoding: NSASCIIStringEncoding, error: &error)

if (error != nil) {
    println("Whoops, something went wrong")
} else {
    println(html)
}    

My problem is that the hebrew characters show up as gibrish: òøëú ùòåú - éåí ùéùé

How can I download the html and get the correct characters?

OmerN
  • 933
  • 2
  • 9
  • 20
  • Most probably, NSASCIIStringEncoding is *not* the correct encoding. Try NSUTF8StringEncoding. – Martin R Sep 11 '15 at 10:47
  • Or have a look at http://stackoverflow.com/a/32051684/1187415 for a method to detect the encoding of the server response automatically. – Martin R Sep 11 '15 at 10:49
  • NSUTF8StringEncoding is not working for me, and the response.textEncodingName doesn't return any value – OmerN Sep 11 '15 at 11:00

1 Answers1

1

This server's response encoding is "Windows Hebrew".

I'm using CoreFoundation to find the correct String encoding equivalent:

let newurl = NSURL(string: "http://www.handasaim.co.il")
var error: NSError?
let encoding = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.WindowsHebrew.rawValue))
let html = String(contentsOfURL: newurl!, encoding: encoding, error: &error)

Result:

תיכון להנדסאים הרצליה ליד אוניברסיטת תל אביב

Swift 2 update

if let newurl = NSURL(string: "http://www.handasaim.co.il") {
    let encoding = CFStringConvertEncodingToNSStringEncoding(UInt32(CFStringEncodings.WindowsHebrew.rawValue))
    if let html = try? String(contentsOfURL: newurl, encoding: encoding) {
        print(html)
    }
}

Note: have a look at Martin R's method to detect the encoding of the server response automatically. My method for this solution was to inspect the headers with curl.

Community
  • 1
  • 1
Eric Aya
  • 69,473
  • 35
  • 181
  • 253