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.