Stringbyevaluatingjavascriptfromstring uses the Stringbyevaluatingjavascriptfromstring method, which needs to be called after the page load in the UIWebView is complete. We drag and drop a UIWebView control on the interface. Load the googlemobile into this control in load, the code is as follows: 1. -(void) viewDidLoad2. {3. [Super viewdidload];4. Webview.backgroundcolor = [Uicolor clearcolor];5. Webview.scalespagetofit =YES;6. Webview.delegate =self;7. Nsurl *url =[[nsurl alloc] initwithstring:@ "HTTP://WWW.GOOGLE.COM.HK/M?GL=CN&HL=ZH_CN&SOURCE=IHP"];8.9. Nsurlrequest *request = [[Nsurlrequest alloc] initwithurl:url];10. [WebView loadrequest:request];11. We can manipulate the interface elements through JavaScript in the Webviewdidfinishload method. 1. Gets the URL of the current page. 1.-(void) Webviewdidfinishload: (UIWebView *) WebView {2. NSString *currenturl = [WebView stringbyevaluatingjavascriptfromstring:@ "Document.location.href"];3. }2, get page title:1. -(void) Webviewdidfinishload: (UIWebView *) WebView {2. NSString *currenturl = [WebView stringbyevaluatingjavascriptfromstring:@ "Document.location.href"];3.4. NSString *title = [WebView stringbyevaluatingjavascriptfromstring:@ "docUment.title "];5. }3, modifies the value of the interface element. 1. NSString *js_result = [WebView stringbyevaluatingjavascriptfromstring:@ "Document.getelementsbyname (' Q ') [0].value = ' Zhu Qilin '; "]; 4. Form submission: 1. NSString *JS_RESULT2 = [WebView stringbyevaluatingjavascriptfromstring:@ "Document.forms[0].submit (); "]; This enables the Google search keyword:" Zhu Qilin "functionality. 5, insert JS code above the function we can encapsulate into a JS function, the function is inserted into the page execution, the code is as follows: 1. [WebView stringbyevaluatingjavascriptfromstring:@ "var script = document.createelement (' script ');" 2. "Script.type = ' text/javascript ';" 3. "Script.text = \" Function myFunction () {"4." var field = document.getelementsbyname (' q ') [0]; " 5. "Field.value= ' Zhu Qilin ';" 6. "Document.forms[0].submit ();" 7. "}\"; " 8. "document.getElementsByTagName (' head ') [0].appendchild (script);"]; 9.10. [WebView stringbyevaluatingjavascriptfromstring:@ "myFunction ();"]; Look at the above code: A, first create a script by JS tag, type ' Text/javascript '. b, and then insert a string in this tag, this string is a function: MyFunction, this function to implement the Google auto-search keyword function. c, and then use Stringbyevaluatingjavascriptfromstring to execute the MyFunction function. Demo: First step open Google Mobile website Second step enter keywords Three-Step Search Summary: This article mainly explains the use of stringbyevaluatingjavascriptfromstring, its function is very powerful, very simple to use, With it we can easily manipulate the page elements in the UIWebView.
Simple JS Operation UIWebView