iphoneobjective-cios7ios6.1tbxml

Encoding ISO 8859-1 to display Spanish text correctly in UITableview


I am facing problem in converting the Spanish text that is fetched from the service in

correct format. Server side they are encoding with ISO-8859-1. It is a xml service. In my

iOS7 app, I am using TBXml parser to parse the data. The code is:

NSString *XMLString = [[NSString alloc] initWithContentsOfURL:urlString encoding:NSISOLatin1StringEncoding error:nil];

TBXML *tbxml = [[TBXML alloc] initWithXMLString:XMLString];

I am parsing this data, but the when there are Spanish characters like "BEBÉS Y" the my

string will be "BEB…S Y" . And "øPor quÈ albergamos a alborotadores?" instead of "¿Por qué albergamos a alborotadores?" . Please help


Solution

  • You should download the XML data as binary (NSData) and let the parser handle the encoding.

    NSData *data = [NSData dataWithContentsOfURL:url];
    
    NSError *error;
    TBXML *tbxml = [TBXML tbxmlWithXMLData:data error:&error];
    

    Note the XML should have the content encoding as the first line so there is no need to specify an encoding in code.