4

I'm trying to feed Mail.app some simple html: lists, bold font, some italics. However, I noticed that if I use characters like £, then Mail.app just doesn't show anything. I realized I need to convert to HTML entities, like £ (full list here: http://www.w3schools.com/tags/ref_entities.asp). I have a partial solution that works for most characters my users have come up with, but it's far from being a solid fix:

- (NSString*) makeValidHTML:(NSString*)str {
  str = [str stringByReplacingOccurrencesOfString:@"£" withString:@"£"];
  str = [str stringByReplacingOccurrencesOfString:@"¢" withString:@"¢"];
  str = [str stringByReplacingOccurrencesOfString:@"¥" withString:@"¥"];
  str = [str stringByReplacingOccurrencesOfString:@"©" withString:@"©"];
  str = [str stringByReplacingOccurrencesOfString:@"®" withString:@"®"];
  str = [str stringByReplacingOccurrencesOfString:@"°" withString:@"°"];
  str = [str stringByReplacingOccurrencesOfString:@"¿" withString:@"¿"];
  str = [str stringByReplacingOccurrencesOfString:@"¡" withString:@"¡"];
  str = [str stringByReplacingOccurrencesOfString:@"‘" withString:@"'"];
  str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
  str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
  str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"""];
  str = [str stringByReplacingOccurrencesOfString:@"“" withString:@"""];
  str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
  str = [str stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
  return str;
}

Is there a standard way to do this without having to list every possible reserved character?

Pablo D.
  • 620
  • 6
  • 15

2 Answers2

4

This class should be helpful to you:
https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m

Link retrieved from this other SO answer:
Converting &amp; to & in Objective-C

Community
  • 1
  • 1
Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
0

I think your main problem is that you're not encoding and declaring your HTML page as UTF-8. While some of the entities you mention are a genuine issue and need to be converted, such as > to &gt; (the code @Joel Martinez linked to will help there), things like the £ symbol will work just fine as they are, provided the page is declared and encoded to be a unicode format such as UTF-8:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
  • Tried this and it didn't make any difference, but thanks for the suggestion. – Pablo D. Sep 07 '11 at 18:24
  • This actually won't work when the server is sending out the charset in the Content-Type HTTP header, because HTTP headers have precedence over meta headers. – duncanwilcox Apr 14 '14 at 09:54