iOS development diary 27-webview and JS interaction
Source: Internet
Author: User
<span id="Label3"></p><p><p>Today Bo Master has a webview and JS Interactive demand, encountered some difficulties, here and we share, hope to progress Together.</p></p><p><p>The interaction between WebView and JS is divided into two ways: 1. JS 2 is called in OC. call OC in JS</p></p><p><p>Web Trial [nsstring stringwithformat:@ "http://cn.bing.com"]</p></p><p><p>I. Invoking JS in OC</p></p><p><p>Directly on the code</p></p><p><p>-(void) webviewdidfinishload: (uiwebview *) WebView</p></p><p><p>{</p></p><p><p>NSString *docstr=[webview stringbyevaluatingjavascriptfromstring:@ "document.documentElement.textContent"];// Gets the Web page content information, obtained here is a JSON string</p></p><p><p></p></p><p><p>NSString *docstr=[webview stringbyevaluatingjavascriptfromstring:[nsstring stringwithformat:@ "alert (1);"]];/ /show and get a pop-up window</p></p><p><p></p></p><p><p>NSString *docstr=[webview stringbyevaluatingjavascriptfromstring:@ "document.location.href"];//get The URL of the current page</p></p><p><p></p></p><p><p>NSString *docstr=[webview stringbyevaluatingjavascriptfromstring:@ "document.title"];//gets The title of the current page</p></p><p><p></p></p><p><p>NSString *docstr=[webview stringbyevaluatingjavascriptfromstring:@ "document.getelementsbyname (' q ') [0].value= '] Mr Lin '; "];/ /modify the value of the element of the current page</p></p><p><p></p></p><p><p>NSString *docstr=[webview stringbyevaluatingjavascriptfromstring:@ "document.forms[0].submit ();"];/ /form Submission</p></p><p><p></p></p><p><p>Static dispatch_once_t oncetoken;</p></p><p><p>Dispatch_once (&oncetoken, ^{</p></p><p><p>[webView stringbyevaluatingjavascriptfromstring:@ "var script = document.createelement (' script ');"</p></p><p><p>"script.type = ' Text/javascript ';"</p></p><p><p>"script.text = \" function myFunction () {"</p></p><p><p>"var field = document.getelementsbyname (' q ') [0];"</p></p><p><p>"field.value= ' Mr Lin ';"</p></p><p><p>"document.forms[0].submit ();"</p></p><p><p>"}\";"</p></p><p><p>"document.getelementsbytagname (' head ') [0].appendchild (script);"];</p></p><p><p>});</p></p><p><p>NSString *docstr=[webview stringbyevaluatingjavascriptfromstring:@ "myFunction ();"];</p></p><p><p></p></p><p><p>NSLog (@ "*******%@", docstr);</p></p><p><p>}</p></p><p><p>PS: Little tricks, The webview comes with attributes that are not able to change the font size color of these requirements, and now give several solutions as Follows:</p></p><p><p>1. The first way: in the WebView proxy method webviewdidfinishload:</p></p><p><p>Font size: [webView stringbyevaluatingjavascriptfromstring:@ "document.getelementsbytagname (' body ') [0]. style.webkittextsizeadjust= ' 12% ' "];</p></p><p><p>Font color: [webView stringbyevaluatingjavascriptfromstring:@ "document.getelementsbytagname (' body ') [0]. Style.webkittextfillcolor= ' Green ' "];</p></p><p><p>Background color: [webView stringbyevaluatingjavascriptfromstring:@ "document.getelementsbytagname (' body ') [0]. Style.background= ' #F6F7F3 ' "];</p></p><p><p>2. The second way: in the following can change the margin size to change the WebView text distance margin size, 13 for the font size, self.strbooktips is the string to be Manipulated.</p></p><p><p>NSString *bookstr = [nsstring stringwithformat:@ "
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service