Skip to content

Instantly share code, notes, and snippets.

@ryanmaxwell
Created March 6, 2012 23:04
Show Gist options
  • Save ryanmaxwell/1989630 to your computer and use it in GitHub Desktop.
Save ryanmaxwell/1989630 to your computer and use it in GitHub Desktop.
Escape NSString so that it can be passed to webview
- (NSString *)stringByEscapingJSONReservedCharacters {
return [[[[[[[[self stringByReplacingOccurrencesOfString:@"\\\\" withString:@"☃"] /* \\ (single slash character) for snowman */
stringByReplacingOccurrencesOfString:@"\\n" withString:@"\\\\n"] /* \n for \\n */
stringByReplacingOccurrencesOfString:@"\\t" withString:@"\\\\t"] /* \t for \\t */
stringByReplacingOccurrencesOfString:@"\\b" withString:@"\\\\b"] /* \b for \\b */
stringByReplacingOccurrencesOfString:@"\\f" withString:@"\\\\f"] /* \f for \\f */
stringByReplacingOccurrencesOfString:@"\\r" withString:@"\\\\r"] /* \r for \\r */
stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\\\\\""] /* \" for \\" */
stringByReplacingOccurrencesOfString:@"☃" withString:@"\\\\\\\\"]; /* snowman for \\\\ (single slash character) */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment