简体   繁体   English

UIWebView无法注入HTML

[英]UIWebView can't inject HTML

I'm trying to put some HTML in a div in a UIWebView. 我试图将一些HTML放在UIWebView的div中。 Although the code seems correct it won't work. 尽管代码看起来正确,但无法正常工作。

This works: 这有效:

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById(\"testdiv\").innerHTML = \"Test text here\""]];

This doesn't: 这不是:

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById(\"testdiv\").innerHTML = \"%@\"", htmlString]];

In the second case 'htmlString' is just some HTML within a blockquote. 在第二种情况下,“ htmlString”只是blockquote中的一些HTML。

Basically when I do it with the HTML it fails. 基本上,当我使用HTML时,它会失败。 My first thought it that the HTML could be invalid but I thought it would still attempt to display it. 我最初认为HTML可能无效,但我认为它仍会尝试显示它。

The 'error' I'm getting as a result of the operation is just an empty (non-nil) string. 由于操作而导致的“错误”只是一个空(非nil)字符串。

Any ideas? 有任何想法吗?

Edit: 编辑:

Here is an example of the HTML that won't display: 这是一个不会显示的HTML示例:

<div style="padding-bottom: 20px;"></div><div><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content=
"text/html charset=us-ascii" />
<title></title>
</head>
<body style=
"word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"
class="">
TestTESTEST<br class="" />
<div apple-content-edited="true" class="">
<div style=
"color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"
class="">
<div class="" style="font-size: 10px;"><br class="" /></div>
</div>
</div>
</body>
</html>
</div>

The htmlString variable is just that HTML within a blockquote tag. htmlString变量就是blockquote标记内的HTML。

Edit 2: 编辑2:

If I take the HTML and put it directly in the document and then load it it works. 如果我采用HTML并将其直接放在文档中,然后加载它,它将起作用。 It's only having issues when I try to insert it via JS. 仅当我尝试通过JS插入它时才有问题。

Temporary solution: 临时解决方案:

As the HTML document I want to insert into is just a template I'm copying it into the code and just building the entire string and loading it into the UIWebView: 由于我要插入的HTML文档只是一个模板,因此将其复制到代码中,然后构建整个字符串并将其加载到UIWebView中:

eg NSString *s = @"<html><body><div1></div1><div2>%@</div2></body></html>", contentToInsert. 例如NSString *s = @"<html><body><div1></div1><div2>%@</div2></body></html>", contentToInsert.

Ideally I still want to get this working as if I can insert the HTML via JS I can use multiple HTML templates and just fit the content into them as needed. 理想情况下,我仍然希望能够像通过JS插入HTML一样工作,可以使用多个HTML模板,并根据需要将内容放入其中。 So if you can find a solution please let me know. 因此,如果您能找到解决方案,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM