将 html 注入 UIWebVeiw

发布于 2024-12-21 16:18:11 字数 902 浏览 0 评论 0原文

我有一个 UIWebView,我正在尝试向其中注入一些 html 代码。我有一个首先加载的模板文件,然后我想通过元素的 ID 更改元素的内容。

这是我的代码,在 webViewDidFinishLoad: 中:

    NSString *injectDetails = [NSString stringWithFormat:
                         @"document.getElementById('details').innerHTML = \"%@\";", searchResult.details];
    [webView stringByEvaluatingJavaScriptFromString:injectDetails];

我已经通过转义引号进行了尝试,但这也不起作用:

    NSString *injectDetails = [NSString stringWithFormat:
                         @"document.getElementById('details').innerHTML = \"%@\";", [searchResult.details stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]];
    [webView stringByEvaluatingJavaScriptFromString:injectDetails];

我注入纯文本,它起作用了。不起作用的 html 是一个很长的字符串,我最初只是使用 [webView loadHTMLString:aDescription baseURL:NULL]; 加载,效果很好。

我需要做什么才能使其与 html 一起工作?

I have a UIWebView that I am trying to inject some html code into. I have a template file that I load first, then I want to alter the content of elements by their ID.

Here is my code, in webViewDidFinishLoad::

    NSString *injectDetails = [NSString stringWithFormat:
                         @"document.getElementById('details').innerHTML = \"%@\";", searchResult.details];
    [webView stringByEvaluatingJavaScriptFromString:injectDetails];

I have tried it by escaping the quotes and that didn't work either:

    NSString *injectDetails = [NSString stringWithFormat:
                         @"document.getElementById('details').innerHTML = \"%@\";", [searchResult.details stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]];
    [webView stringByEvaluatingJavaScriptFromString:injectDetails];

I cab inject plain text, and it works. The html that doesn't work is a lengthy string that I had originally simply loaded using [webView loadHTMLString:aDescription baseURL:NULL]; That worked fine.

What do I need to do make this work with html?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼角的笑意。 2024-12-28 16:18:11

当我尝试向文档头部添加样式时,我遇到了同样的问题。所以我只是用 \\" 转义了所有双配额(在脚本中看起来像 \" )。参见下面的代码:

NSString *js = @" <style type=\\\"text/css\\\"> * { -webkit-touch-callout: none; -webkit-user-select: none; } </style> ";
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.head.innerHTML = document.head.innerHTML+\"%@\"",js]];

I just had same problem when trying to add style to document's head. So I just escaped all double quotas with \\" (which looks like \" in script). See code below:

NSString *js = @" <style type=\\\"text/css\\\"> * { -webkit-touch-callout: none; -webkit-user-select: none; } </style> ";
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.head.innerHTML = document.head.innerHTML+\"%@\"",js]];
无声静候 2024-12-28 16:18:11

尝试使用以下代码行来实现此目的。

NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *HTMLData = @"Hello this is a test<img src="sample.jpg" alt="" width="100" height="100" />";

[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];

1号线
这将获取主包根文件夹的路径。

2号线
我们需要双斜杠才能在 UIWebView 中正常工作,因此我们正在搜索“/”的所有实例并将其替换为“//”

第 3 行
与上面的处理相同,但我们正在搜索一个空格并将其替换为相当于 %20

第 4 行 的 HTML
这是将 UIWebView 的一些示例数据放在一起,您将看到我们已将图像源设置为我们的“sample.jpg”文件,因为它位于根文件夹中,如果它位于名为“images”的文件夹中,我们需要将源设置为“images//sample.jpg”。记住双砍!

5号线
这与我在教程中的示例几乎相同,尽管我们将 baseURL 设置为应用程序包的根目录,这允许我们相对引用所有内容而不是绝对路径。

使用上面的方法你可以做到这一点..如果发生任何问题请告诉我..

try the following line of code to make this happens.

NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

NSString *HTMLData = @"Hello this is a test<img src="sample.jpg" alt="" width="100" height="100" />";

[webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]];

Line 1
This will get the path to the main bundle root folder.

Line 2
We need the slashes to be double slashed to work correctly in the UIWebView, so we are searching for all instances of “/” and replacing it with “//”

Line 3
Same deal as above but we are searching for a space and replacing it with the HTML equivalent of %20

Line 4
This is putting some sample data together for out UIWebView, you will see that we have set the images source to just our “sample.jpg” file as it is sitting in the root folder, if it was in a folder called “images” we would need to set the source to “images//sample.jpg”. Remember the double slashing!

Line 5
This is pretty much the same as my example in the tutorial although we are setting the baseURL to the root of our application bundle, this allows us to reference everything relatively instead of absolute paths.

Using the above you can do this.. let me know if any issue occurs..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文