Objective-C interaction with Javascript in iOS development

Source: Internet
Author: User

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.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.