Objective-C interaction with JavaScript

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.

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.

 

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.