あなたが引用する方法は、特定の文字エンコーディング(UTF-8やASCIIなど)でディスクからファイルを読み取ります。URL や HTML エスケープとは関係ありません。
URL パーセント エスケープを追加する場合は、次の方法が必要です。
[myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
このメソッドに関するドキュメントを必ず読んでください。何をエスケープし、何をそのままにしておくかについて、特定の微妙な点があるからです。場合によっては、より複雑で柔軟なCFURLCreateStringByAddingPercentEscapes()
. (そうする場合は、キャストできることに注意してくださいCFStringRef
。NSString *
また、その逆も可能です。)
XML/HTML スタイルのエンティティ エスケープを実行するために私が知っていることは何も組み込まれていませんが、この関数は基本を処理する必要があります。
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];
}