質問を完全に理解しているかどうかはわかりませんが、アプリ バンドルから HTML ファイルを読み込んで、アプリから別の HTML コンテンツを動的に挿入できるかどうかを尋ねていると思います...そうですか?
もしそうなら、あなたは確かにそれを行うことができます。
まず、コンテンツを変更可能な文字列にロードします。
NSString *htmlPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"combo.html"];
NSError *error;
NSMutableString *htmlContent = [NSMutableString stringWithContentsOfFile: htmlPath
encoding: NSUTF8StringEncoding
error: &error];
コンボ ボックスに新しいオプション/値を挿入するには、次のようにします。
// look for the start of the combo box called "food"
NSRange range = [htmlContent rangeOfString: @"select name=\"food\""
options: NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
// search for the end tag </select>
range.length = htmlContent.length - range.location;
NSRange end = [htmlContent rangeOfString: @"</select>"
options: NSCaseInsensitiveSearch
range: range];
if (end.location != NSNotFound) {
NSString *newChoice = @"Awesome!"; // get this dynamically however you want
NSString *newOption =
[NSString stringWithFormat: @"<option value=\"4\">%@</option>\n", newChoice];
[htmlContent insertString: newOption atIndex: end.location];
NSLog(@"htmlContent = %@", htmlContent);
}
}
既存のコンボ ボックス オプションの表示値を変更する場合は、次のようなコードを使用します。
NSString *optionTwoHtml = @"<option value=\"2\">";
NSRange optionTwo = [htmlContent rangeOfString: optionTwoHtml
options: NSCaseInsensitiveSearch];
if (optionTwo.location != NSNotFound) {
int start = optionTwo.location + optionTwoHtml.length;
// search for the end tag </option>
optionTwo.length = htmlContent.length - optionTwo.location;
NSRange end = [htmlContent rangeOfString: @"</option>"
options: NSCaseInsensitiveSearch
range: optionTwo];
if (end.location != NSNotFound) {
NSString *newValue = @"Better Than Best!";
NSRange oldRange = NSMakeRange(start, end.location - start);
[htmlContent replaceCharactersInRange: oldRange
withString: newValue];
NSLog(@"htmlContent = %@", htmlContent);
}
}
このコードは、何ができるかを示しているだけであることに注意してください。パフォーマンスのために最適化されていません。あなたが示した HTML コンテンツは非常に小さいので、解析コードがどれほど効率的であるかは問題ではありません。はるかに大きな HTML ファイルを使用する場合は、少し最適化することをお勧めします。