iOS开发 - 利用js去除webview广告

找到要取出内容方法:
浏览器设置 -> 更多工具 -> 开发者工具 -> 手机模式(左下角 手机按钮)
点击手机图标左边那个搜索框 -> 然后点击你要隐藏的控件
选择到你要选择到的控件 -> 然后左下角代码就被选中了.
其中右下角中是这个控件对应的css代码,在其中添加一行display:none,这时这个选中的控件就被隐藏起来了.但是我们主要通过javascript去操作这个控件
选择除掉内容

利用js去除webview广告

最后代码如下:

//
//  MainViewController.m
//  webview
//
//  Created by ??? on 15/11/24.
//  Copyright © 2015年 ???. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()<UIWebViewDelegate>

@property (nonatomic,strong)UIWebView *webView;


@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self webView];
}

- (UIWebView *)webView{
    if (!_webView) {
        NSString *urlStr = @"http://group.haodou.com/topic-327282.html";
        
        self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        self.webView.delegate = self;
        self.webView.backgroundColor = [UIColor redColor];
        [self.view addSubview:_webView];
        
        NSURL *url=[NSURL URLWithString:urlStr];
        NSURLRequest *request=[[NSURLRequest alloc] initWithURL:url];
        [self.webView loadRequest:request];
    }
    return _webView;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('main_mu_bar')[0].style.display = 'NONE'"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

编辑文字方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值