The method you cite reads a file from disk with a given character encoding (such as UTF-8 or ASCII). It has nothing to do with URL or HTML escaping.
If you want to add URL percent escapes, you want this method:
[myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Make sure you read the documentation about this method, because there are certain subtleties about what it escapes and what it leaves alone. In some cases, you may have to use the more complex, but more flexible, CFURLCreateStringByAddingPercentEscapes()
. (If you do, note that you can cast CFStringRef
to NSString *
and vice versa.)
There's nothing built in that I know of to do XML/HTML-style entity escaping, but this function ought to handle the basics:
NSString * convertToXMLEntities(NSString * myString) {
NSMutableString * temp = [myString mutableCopy];
[temp replaceOccurrencesOfString:@"&"
withString:@"&"
options:0
range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"<"
withString:@"<"
options:0
range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@">"
withString:@">"
options:0
range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"\""
withString:@"""
options:0
range:NSMakeRange(0, [temp length])];
[temp replaceOccurrencesOfString:@"'"
withString:@"'"
options:0
range:NSMakeRange(0, [temp length])];
return [temp autorelease];
}