¿Cómo convierto de manera confiable caracteres especiales como £ a equivalentes HTML usando Cocoa?

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?

preguntado el 27 de agosto de 11 a las 23:08

2 Respuestas

This class should be helpful to you:

Link retrieved from this other SO answer:
Conversión de & a & en Objective-C

contestado el 23 de mayo de 17 a las 14:05

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 > a &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" />

Respondido 28 ago 11, 03:08

Tried this and it didn't make any difference, but thanks for the suggestion. - Pablo diaz

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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.