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,CodeAs 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. the string is a function:MyfunctionThis function enables Google to automatically search for keywords.
C. UseStringbyevaluatingjavascriptfromstring executes the myfunction.
Demo:
Step 1 open the Google mobile website
Step 2 enter keywords
Step 3 search
Summary:This articleArticleIt 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.