Reputation: 6610
I'm downloading html pages through NSURLConnection which gives me NSData representation of the html page at the end. I need to convert this NSData to NSString in order to parse it. My problem is that this page uses special characters like "ě š č ř ž ý á á í é" etc. and when i convert NSData to NSString i get some jibrish like "–ì¬≠" instead of "í" etc. I know the problem is with encoding, but i have no idea how to overcome this problem... I don't know much about encoding but i found that the site uses utf-8 (and those characters are in czech), so i'm really in dead end...
Here is my code :
NSString* docHTML = [[NSString alloc] initWithBytes: [self.receivedData bytes] length:[self.receivedData length] encoding: NSUTF8StringEncoding];
Thanks for any help
Upvotes: 0
Views: 836
Reputation: 1524
You can try something like:
NSString *docHTML;
docHTML = [[NSString alloc] initWithData:self.receivedData encoding:NSASCIIStringEncoding];
if (!docHTML)
{
NSLog(@"ASCII not working, will try utf-8!");
docHTML = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
}
//Do stuff with docHTML
Good luck!
Upvotes: 0
Reputation: 299575
Your webpage likely does not encode its content in UTF-8. You need to use the same encoding that the webpage did.
EDIT: Also note that receivedBytes
isn't valid until the connection has completed. You can't convert data into strings in the middle of didReceiveData.
As a side note, it's easier to use initWithData:encoding:
for this.
Upvotes: 3