Uiwebview is one of the most commonly used sdks for iOS. It has a stringbyevaluatingjavascriptfromstring method that can embed Javascript into the page. Through this method, we can interact with webpage elements in uiwebview in IOS.
Uiwebview is one of the most commonly used sdks for iOS. It has a stringbyevaluatingjavascriptfromstring method that can embed Javascript into the page. Through this method, we can interact with webpage elements in uiwebview in IOS.
Stringbyevaluatingjavascriptfromstring
To use the stringbyevaluatingjavascriptfromstring method, call the method after the page in uiwebview is loaded. We drag and drop a uiwebview control on the interface. Load Google mobile into this control in load. The Code is as follows:
-(Void) viewdidload
{
[Super viewdidload];
Webview. backgroundcolor = [uicolor clearcolor];
Webview. scalespagetofit = yes;
Webview. Delegate = self;
Nsurl * url = [[nsurl alloc] initwithstring: @ "http://www.google.com.hk/M? GL = cn & HL = zh_cn & source = IHP "];
Nsurlrequest * request = [[nsurlrequest alloc] initwithurl: url];
[Webview loadrequest: request];
}
We can use JavaScript to operate interface elements in the webviewdidfinishload method.
1. Obtain the URL of the current page.
-(Void) webviewdidfinishload :( uiwebview *) webview
{
Nsstring * currenturl = [webview stringbyevaluatingjavascriptfromstring: @ "document. Location. href"];
}
2. Obtain the page title:
-(Void) webviewdidfinishload :( uiwebview *) webview
{
Nsstring * currenturl = [webview stringbyevaluatingjavascriptfromstring: @ "document. Location. href"];
Nsstring * Title = [webview stringbyevaluatingjavascriptfromstring: @ "document. Title"];
}
3. Modify the value of the interface element.
Nsstring * js_result = [webview stringbyevaluatingjavascriptfromstring: @ "document. getelementsbyname ('q') [0]. value = 'zhu Yulin ';"];
4. Form submission:
Nsstring * js_result2 = [webview stringbyevaluatingjavascriptfromstring: @ "document. Forms [0]. Submit ();"];
In this way, you can search for the keyword "Zhu Yulin" on Google.
5. Insert JS Code
The above functions can be encapsulated into a JS function and inserted into the page for execution. The Code is as follows:
[Webview stringbyevaluatingjavascriptfromstring: @ "Var script = Document. createelement ('script ');"
"Script. type = 'text/JavaScript ';"
"Script. Text = \" function myfunction (){"
"Var field = Document. getelementsbyname ('q') [0];"
"Field. value = 'zhu Yulin ';"
"Document. Forms [0]. Submit ();"
"}\";"
"Document. getelementsbytagname ('head') [0]. appendchild (SCRIPT);"];
[Webview stringbyevaluatingjavascriptfromstring: @ "myfunction ();"];
Look at the above Code:
A. First, create a script tag using Js. The type is 'text/JavaScript '.
B. Insert a string into the tag. This string is a function: myfunction, which enables Google to automatically search for keywords.
C. Use stringbyevaluatingjavascriptfromstring to execute the myfunction.
Demo:
Step 1 open the Google mobile website
Step 2 enter keywords
Step 3 search
Summary: This article mainly explains the usage of stringbyevaluatingjavascriptfromstring. It is very powerful and easy to use. Through it, we can easily operate the page elements in uiwebview.