UIWebView和网页的交互(OC中调执行JS)

UIWebView和网页的交互(OC中调执行JS)
- (void)viewDidLoad
{
    [
super viewDidLoad];
   
// 1.webView
   
UIWebView *webView = [[UIWebView alloc] init];
    webView.
frame = self.view.bounds;
    webView.
delegate = self;
   
// 伸缩页面至填充整个webView
    webView.
scalesPageToFit = YES;
   
// 隐藏scrollView
    webView.
scrollView.hidden = YES;
    [
self.view addSubview:webView];
   
   
// 2.加载网页
   
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.dianping.com/tuan/deal/5501525"]];
    [webView
loadRequest:request];
   
   
// 3.创建
   
UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [loadingView
startAnimating];
    loadingView.
center = CGPointMake(160, 240);
    [
self.view addSubview:loadingView];
   
self.loadingView = loadingView;
}

// OC中调用JS
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{

   
NSMutableString *js1 = [NSMutableString string];
   
// 0.删除顶部的导航条
    [js1
appendString:@"var header = document.getElementsByTagName(‘header‘)[0];"];
    [js1
appendString:@"header.parentNode.removeChild(header);"];
   
   
// 1.删除底部的链接
    [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.删除浮动的广告
        [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];
       
       
// 显示scrollView
        webView.
scrollView.hidden = NO;
       
       
// 删除圈圈
        [
self.loadingView removeFromSuperview];
    });
}
注意:
从网络上获取数据,如果不想看到某种效果,并且网页存放在服务器端,里面的JS和Html代码,没有办法修改,可以在UIWebView的代理方法执行JS代码,去掉不想看到的效果。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。