简体   繁体   English

获取UIWebView的当前html内容?

[英]Get the current html content of UIWebView?

I load a html template from the NSBundle using UIWebView 's loadRequest method. 我使用UIWebViewloadRequest方法从NSBundle加载html模板。 The html template itself have large number of textboxes (say 20). html模板本身具有大量的文本框(例如20个)。 After I fill all the textboxes I want the html with these filled values. 填充所有文本框后,我希望html具有这些填充值。 I tried, 我试过了,

[myWebView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"]
//even body.outerHTML, documentElement.innerHTML won't work

But it is giving me the raw html template before these values filled. 但这给了我原始的html模板,这些值被填充了。 How can I do it? 我该怎么做? I am thinking this to do this, by reading the content of each textboxes, by javascript, 我正在考虑通过使用javascript阅读每个文本框的内容来做到这一点,

[myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('name').value"]

and then read and replace the strings of the html. 然后阅读并替换html的字符串。

Is there any other better way to do this? 还有其他更好的方法吗? Or is there any javascript methods to read the current content of the webpage? 还是有任何JavaScript方法可读取网页的当前内容?

You are completely right that .innerHTML and .outerHTML won't give you the updated state of the DOM. 完全正确的是.innerHTML.outerHTML不会为您提供DOM的更新状态。

However, since your are already into querying by Javascript, you can create a JSON string containing a list of all the currently entered values: 但是,由于您已经在使用Javascript查询,因此可以创建一个JSON字符串,其中包含所有当前输入值的列表:

NSString *jsonString = [myWebView stringByEvaluatingJavaScriptFromString:@"(function(fields) { var O=[]; for(var i=0; i<fields.length;i++) {O.push(fields[i].value);} return JSON.stringify(O); })(document.querySelectorAll('input[type="text"]'))"];

You can then parse that in your native code an if it worked it should come out as an NSArray of strings. 然后,您可以在本机代码中解析它是否起作用,它应该以字符串NSArray的形式出现。

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

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