1

I have an iOS 7 app that is calling a webservice that returns XML with encoded characters. At first there was only a few of them, so I was just replacing them like so:

- (NSString *)convertEncodedCharacters:(NSString *)html
{        
    // Handle escape characters
    html = [html stringByReplacingOccurrencesOfString:@"’" withString:@"'"];
    html = [html stringByReplacingOccurrencesOfString:@"‘" withString:@"‘"];
    html = [html stringByReplacingOccurrencesOfString:@"—" withString:@"-"];
    html = [html stringByReplacingOccurrencesOfString:@""" withString:@"\""];
    html = [html stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x3C;" withString:@"<"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x3E;" withString:@">"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x2013;" withString:@"–"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x201C;" withString:@"“"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x201D;" withString:@"”"];
    html = [html stringByReplacingOccurrencesOfString:@"&#x201A;" withString:@"‚"];
    html = [html stringByReplacingOccurrencesOfString:@"&#xA0;" withString:@" "];

    return html;
}

But now the content has gotten more complex and I keep running into new characters. Is there a way to just convert all of the encoded characters into human readable ones?

lehn0058
  • 19,977
  • 15
  • 69
  • 109
  • You can set the encoding to NSUTF8StringEncoding, when the data is received. – 7c9d6b001a87e497d6b96fbd4c6fdf Mar 15 '14 at 19:32
  • 3
    Have a look into http://stackoverflow.com/questions/659602/objective-c-html-escape-unescape – cweinberger Mar 15 '14 at 19:32
  • Repeatedly calling `stringByReplacingOccurencesOfString:` is super wasteful; it's doing an allocation for _every_ entity. You want to [use `NSScanner` for this](https://github.com/woolsweater/NSString-WSSHTMLEntityConversion), or at least use a mutable string. – jscs Mar 15 '14 at 19:43
  • cweinberger, that is close, but the encoding is different. Using that code just returns a bunch of new lines and spaces for me. – lehn0058 Mar 15 '14 at 21:23
  • cweinberger, actually, that did work once I changed the encoding to NSUTF16StringEncoding. Thank you! If you move your comment to an answer, I will mark yours as correct. – lehn0058 Mar 15 '14 at 21:28

1 Answers1

0

check this class from GitHub, exactly what you are looking for

https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m

haz azezan
  • 27
  • 1
  • 10