Interaction between iOS UIWebView and webpage (calls JS in OC) and uiwebviewoc

Source: Internet
Author: User

Interaction between iOS UIWebView and webpage (calls JS in OC) and uiwebviewoc

Interaction between UIWebView and webpage (calls JS in OC)
-(Void) viewDidLoad
{
[Super viewDidLoad];
// 1. webView
UIWebView * webView = [[UIWebView alloc] init];
WebView. frame = self. view. bounds;
WebView. delegate = self;
// Scale the page to fill the entire webView
WebView. scalesPageToFit = YES;
// Hide the scrollView
WebView. scrollView. hidden = YES;
[Self. view addSubview: webView];

// 2. Load the webpage
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;
}
// Call JS in OC
# Pragma mark-UIWebViewDelegate
-(Void) webViewDidFinishLoad :( UIWebView *) webView
{
NSMutableString * js1 = [NSMutableString string];
// 0. Delete the navigation bar on the top
[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 floating ads
[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];

// Display scrollView
WebView. scrollView. hidden = NO;

// Delete the circle
[Self. loadingView removeFromSuperview];
});
}
Note: If you do not want to see any effect and the webpage is stored on the server side, the JS and Html code in it cannot be modified, you can execute JS code in the proxy method of UIWebView to remove the effect you don't want to see.

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.