Reading HTML content from a UIWebView after editing it

I am trying to upload an HTML page with text fields to a web view, edit the text in the text fields and then retrieve the HTML data in this web view.

I use this code to load the page:

NSString* path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"htm"]; NSData* xmlData = [NSData dataWithContentsOfFile:path]; [web loadData:xmlData MIMEType:@"text/html" textEncodingName:nil baseURL:nil]; 

And this code to get the content and install it again in webview:

 NSString *myText = [web stringByEvaluatingJavaScriptFromString:@"document.all[0].innerHTML"]; data = [myText dataUsingEncoding:NSUTF8StringEncoding]; [web loadData:data MIMEType:@"text/html" textEncodingName:nil baseURL:nil]; 

The problem is that the text that I pasted into the text box is not coming back from the web view.

I tried document.body.innerHTML and document.body.outerHTML , they all return HTML page data the way I loaded it.

Is there anyway to get the data from the HTML page containing the text that I inserted there?

+4
source share
1 answer

Try using stringByEvaluatingJavaScriptFromString . If you use jquery, you should be able to use an expression that returns the value of your text field, i.e.:

 NSString* text = [webView stringByEvaluatingJavaScriptFromString:@"$('#myTextFieldId').val();"]; 
+1
source

Source: https://habr.com/ru/post/1390468/


All Articles