IOS UIWebView and Web page interaction (OC in tune to execute JS)

Source: Internet
Author: User

UIWebView and Web page interaction (OC-in-Tune execution JS)
-(void) viewdidload
{
[Super Viewdidload];
1.webView
UIWebView *webview = [[UIWebView alloc] init];
Webview.frame = Self.view.bounds;
Webview.delegate = self;
Stretch the page to fill the entire WebView
Webview.scalespagetofit = YES;
Hide ScrollView
WebView.scrollView.hidden = YES;
[Self.view Addsubview:webview];

2. Loading Web pages
Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@ "http://m.dianping.com/tuan/deal/5501525 "]];
[WebView Loadrequest:request];

3. Create
Uiactivityindicatorview *loadingview = [[Uiactivityindicatorview alloc] Initwithactivityindicatorstyle: Uiactivityindicatorviewstylewhitelarge];
[Loadingview startanimating];
Loadingview.center = Cgpointmake (160, 240);
[Self.view Addsubview:loadingview];
Self.loadingview = Loadingview;
}
Invoking JS in OC
#pragma mark-uiwebviewdelegate
-(void) Webviewdidfinishload: (UIWebView *) WebView
{
nsmutablestring *js1 = [nsmutablestring string];
0. Remove the top navigation bar
[Js1 appendstring:@ "var header = document.getElementsByTagName (' header ') [0];"];
[Js1 appendstring:@ "Header.parentNode.removeChild (header);"];

1. Delete the link at the bottom
[Js1 appendstring:@ "var footer = document.getelementsbytagname (' footer ') [0];"];
[Js1 appendstring:@ "footer.parentNode.removeChild (footer);"];
[WebView STRINGBYEVALUATINGJAVASCRIPTFROMSTRING:JS1];

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (0.25 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
nsmutablestring *js2 = [nsmutablestring string];
2. Delete a floating ad
[Js2 appendstring:@ "var list = document.body.childNodes;"];
[Js2 appendstring:@ "var len = list.length;"];
[Js2 appendstring:@ "var banner = list[len-1];"];
[Js2 appendstring:@ "Banner.parentNode.removeChild (banner);"];
[WebView STRINGBYEVALUATINGJAVASCRIPTFROMSTRING:JS2];

Show ScrollView
WebView.scrollView.hidden = NO;

Delete Circle
[Self.loadingview Removefromsuperview];
});
}
Note: Get data from the network, if you do not want to see an effect, and the Web page is stored on the server side, the inside of the JS and HTML code, there is no way to modify, you can execute the JS code in the UIWebView proxy method, remove the effect you do not want to see.

IOS UIWebView and Web page interaction (OC in tune to execute 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.