La méthode que vous citez lit un fichier à partir du disque avec un codage de caractères donné (par exemple UTF-8 ou ASCII). Cela n'a rien à voir avec l'échappement d'URL ou de HTML.
Si vous voulez ajouter échappe URL pour cent, vous voulez cette méthode:
[myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Assurez-vous de lire la documentation sur cette méthode, car il y a certaines subtilités sur ce qu'il échappe et ce qu'il laisse seul. Dans certains cas, vous devrez peut-être utiliser le plus complexe, mais plus flexible, CFURLCreateStringByAddingPercentEscapes()
. (Si vous faites, notez que vous pouvez lancer CFStringRef
-NSString *
et vice versa.)
Il n'y a rien construit que je sache faire l'entité XML/style HTML échapper, mais cette fonction doit gérer les bases:
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];
}
Lisez les autres réponses pour des méthodes pour citer des chaînes comme XML (par exemple, remplacement d'entité et <à <) –