I'm developing an application for the iPhone that has inApp-mail sending capabilities. So far so good, but now I want to avoid html-injections as some parts of the mail are user-generated texts.
Basically I search for something like this:
// inits
NSString *sourceString = [NSString stringWithString:@"Hello world! Grüße dich Welt <-- This is in German."];
// ----- THAT'S WHAT I'M LOOKING FOR
// pseudo-code |
// V
NSString *htmlEncodedString = [sourceString htmlEncode];
// log
NSLog(@"source string: %@", sourceString);
NSLog(@"encoded string: %@", htmlEncodedString);
Expected output
source string: Hello world! Grüße dich Welt <-- This is in German.
encoded string: Hello world! Grüße dich Welt <-- This is in German.
I already googled and looked through several of SO's questions and answers, but all of them seem to be related to URL-encoding and that's not what I really need (I tried stringByAddingPercentEscapesUsingEncoding
with no luck - it creates %C3%BC out of an 'ü' that should be an ü).
A code sample would be really great (correcting mine?)...
--
Thanks in advance,
Markus
A little improvement on @Markus' code [Change <br /> to <p></p>, escape multiple spaces]
I have been looking for a similar solution and this did the job for me
//Substring is now html entity encoded
I am using some of the features that is used when saving plist files. I hope this helps.
I'm expanding @Markus answer, because my case is i'm sending JSON string, so i need to added some escape, these are my function :
note : the exception reference from w3schools. https://www.w3schools.com/tags/ref_urlencode.asp
Assuming the character encoding of the email supports Unicode - say UTF-8 - could you not just find and replace the occurrences of
<
,>
, and&
with<
,>
, and&
?Thanks @all. I ended up using my own implementation:
Check out my NSString category for HTML. Here are the methods available: