HTML entity encoding (convert '<' to '<') on iPhone in objective-c

后端 未结 6 1307
忘了有多久
忘了有多久 2020-12-09 11:19

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

6条回答
  •  猫巷女王i
    2020-12-09 12:04

    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

    - (NSString*)convertStringToHTMLEscape:(NSString*)stringContent
    {
        stringContent = [stringContent stringByReplacingOccurrencesOfString:@"{" withString:@"%7B"];
        stringContent = [stringContent stringByReplacingOccurrencesOfString:@"}" withString:@"%7D"];
        stringContent = [stringContent stringByReplacingOccurrencesOfString:@"[" withString:@"%5B"];
        stringContent = [stringContent stringByReplacingOccurrencesOfString:@"]" withString:@"%5D"];
        stringContent = [stringContent stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        stringContent = [stringContent stringByReplacingOccurrencesOfString:@"\"" withString:@"%22"];
        stringContent = [stringContent stringByReplacingOccurrencesOfString:@"\\" withString:@"%5C"];
        stringContent = [stringContent stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
    
        return stringContent;
    }
    

提交回复
热议问题