Interaction between UIWebView and webpage (JavaScript code is called in OC) and uiwebviewoc
Interaction between UIWebView and webpage (calls JS in OC)
-(Void) viewDidLoad
{
[SuperviewDidLoad];
// 1. webView
UIWebView * webView = [[UIWebViewalloc] 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. viewaddSubview: webView];
// 2. Load the webpage
NSURLRequest * request = [NSURLRequestrequestWithURL: [NSURLURLWithString: @ "http://m.dianping.com/tuan/deal/5501525"];
[WebView loadRequest: request];
// 3. Create
UIActivityIndicatorView * loadingView = [[UIActivityIndicatorViewalloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
[LoadingView startAnimating];
LoadingView. center = CGPointMake (160,240 );
[Self. viewaddSubview: loadingView];
Self. loadingView = loadingView;
}
// Call JS in OC
# Pragma mark-UIWebViewDelegate
-(Void) webViewDidFinishLoad :( UIWebView *) webView
{
NSMutableString * js1 = [NSMutableStringstring];
// 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 = [NSMutableStringstring];
// 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. loadingViewremoveFromSuperview];
});
}
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.