Simple interaction between IOS web and JS

Source: Internet
Author: User

when we encapsulate a Web page, we often encounter the need to add data to the controls on the page, but it becomes difficult to add. I recently encountered such a thing in the development, after the solution, to share with you.

Take Bing website as an example

[Web loadrequest:

[Nsurlrequestrequestwithurl:

[Nsurl urlwithstring:@ "http://www.bing.com/?FORM=Z9FD1"]];

Here's the main code, just a few words

Gets the title of the current page

NSString *title =

[WebView

Stringbyevaluatingjavascriptfromstring:

@ "Document.title"];

NSLog (@ "title====%@", title);

Get current URL

NSString *url =

[WebView

Stringbyevaluatingjavascriptfromstring: @ "Document.location.href"];

NSLog (@ "url===%@", URL);

Get the page code

NSString *html =

[WebView

Stringbyevaluatingjavascriptfromstring:

@ "Document.documentElement.innerHTML"];

NSLog (@ "html====%@", HTML);

Stitching strings to find the control price and assign a value based on the page name

NSString *str = @ "With _ Simple book";

NSString *jsstr =

[NSString stringWithFormat:

@ "Document.getelementsbyname (' Q ') [0].value = ('%@ ');", str];

[WebView STRINGBYEVALUATINGJAVASCRIPTFROMSTRING:JSSTR];

The following is all the code, Commit+c Commit+v can be run directly

@interface Viewcontroller ()

@property (Weak, nonatomic) UIWebView *web;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

UIWebView *web =

[[UIWebView alloc] initWithFrame:

[UIScreen mainscreen].bounds];

UIButton *back =

[[UIButton alloc] Initwithframe:cgrectmake

(0, 0, self.view.frame.size.width,self.view.frame.size.width/7.5)];

[Back SetBackgroundColor: [Uicolor Orangecolor];

[Back settitle:@ ' back ' forstate:uicontrolstatenormal];

[Back Addtarget:self

Action: @selector (back)

Forcontrolevents:uicontroleventtouchupinside];

Take Bing as an example

[Web loadrequest:

[Nsurlrequest Requestwithurl:

[Nsurl URLWithString:

@ "Http://www.bing.com/?FORM=Z9FD1"]];

Web.delegate = self;

Web.scalespagetofit =yes;

Web.scrollView.delegate = self;

Self.web = web;

[Self.view Addsubview:web];

[Self.view Addsubview:back];

}

#pragma Mark---Delegate

-(void) Webviewdidstartload:

(UIWebView *) webview{

NSLog (@ "Start loading--");

}

-(void) Webviewdidfinishload:

(UIWebView *) WebView {

NSLog (@ "Load complete---");

Gets the title of the current page

NSString *title =

[WebView

Stringbyevaluatingjavascriptfromstring:

@ "Document.title"];

NSLog (@ "title====%@", title);

Get current URL

NSString *url =

[WebView

Stringbyevaluatingjavascriptfromstring:

@ "Document.location.href"];

NSLog (@ "url===%@", URL);

Get the page code

NSString *html =

[WebView

Stringbyevaluatingjavascriptfromstring:

@ "Document.documentElement.innerHTML"];

NSLog (@ "html====%@", HTML);

Stitching strings to find the control price and assign a value based on the page name

NSString *str = @ "With _ Simple book";

NSString *jsstr =

[NSString stringWithFormat:

@ "Document.getelementsbyname (' Q ') [0].value = ('%@ ');", str];

[WebView STRINGBYEVALUATINGJAVASCRIPTFROMSTRING:JSSTR];

}

-(void) WebView:

(UIWebView *) WebView

Didfailloadwitherror: (Nserror *) Error {

NSLog (@ "Load failed ===%@", error);

}

When the page location is top not allowed to continue dropdown

-(void) Scrollviewdidscroll:

(Uiscrollview *) ScrollView {

if (SELF.WEB.FRAME.ORIGIN.Y = = 0) {

Self.web.scrollView.bounces = NO;

Return

}

}

WebView every page jump will be executed, here to get the data you want

-(BOOL) WebView:

(UIWebView *) WebView

Shouldstartloadwithrequest: (nsurlrequest *) request

Navigationtype: (uiwebviewnavigationtype) Navigationtype {

NSLog (@ "page jump");

return YES;

}

Return

-(void) Back {

[Self.web GoBack];

}

Simple interaction between IOS web and JS

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.