第一種:UIWebView與js交互
1溯饵、初始化
self.webView2 = [[UIWebView alloc] initWithFrame:self.view.frame];
_webView2.delegate = self;
NSURLRequest *url = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://112.124.54.19/Score/cet.html"]];
[self.webView2 loadRequest:url];
[self.view addSubview:self.webView2];
self.whiteView.hidden = NO;//一個白色的View主要是遮蓋未修改的html界面
2知残、實現(xiàn)代理方法
-(void)webViewDidFinishLoad:(UIWebView *)webView{
//刪除圖片
NSString * p = @"var p = document.getElementsByClassName('pic');";
NSString * r = @"p[0].remove();";
NSString * java = [NSString stringWithFormat:@"%@%@",p,r];
//替換文字
NSString * text_p = @"var p = document.getElementsByClassName('tips-hold');";
NSString * text_r = @"p[0].innerHTML='XXX四六級英語查詢';";
NSString * text_java = [NSString stringWithFormat:@"%@%@",text_p,text_r];
//修改按鈕顏色
NSString * btn_p = @"var p = document.getElementById('submit-btn');";
NSString * btn_r = @"p.style.backgroundColor='#4CA4FE';";
NSString * btn_java = [NSString stringWithFormat:@"%@%@",btn_p,btn_r];
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
[context evaluateScript:java];
[context evaluateScript:text_java];
[context evaluateScript:btn_java]腔召;
self.whiteView.hidden = YES;
}
第二種:WKWebView與js交互
1篷朵、初始化
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame];
webView.allowsBackForwardNavigationGestures = YES;
NSURLRequest *url = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://112.124.54.19/Score/cet.html"]];
[webView loadRequest:url];
webView.navigationDelegate = self;
[self.view addSubview:webView];
self.webView = webView;
2杰扫、實現(xiàn)代理方法
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
//刪除圖片
NSString * p = @"var p = document.getElementsByClassName('pic');";
NSString * r = @"p[0].remove();";
NSString * java = [NSString stringWithFormat:@"%@%@",p,r];
//替換文字
NSString * text_p = @"var p = document.getElementsByClassName('tips-hold');";
NSString * text_r = @"p[0].innerHTML='XXX四六級英語查詢';";
NSString * text_java = [NSString stringWithFormat:@"%@%@",text_p,text_r];
//修改按鈕顏色
NSString * btn_p = @"var p = document.getElementById('submit-btn');";
NSString * btn_r = @"p.style.backgroundColor='#4CA4FE';";
NSString * btn_java = [NSString stringWithFormat:@"%@%@",btn_p,btn_r];
[webView evaluateJavaScript:java completionHandler:nil];
[webView evaluateJavaScript:text_java completionHandler:nil];
[webView evaluateJavaScript:btn_java completionHandler:nil];
self.whiteView.hidden = YES;
}
另外補充:顯示加載進度條
-(UIProgressView *)progressView{
if (!_progressView) {
_progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 2)];
_progressView.progressTintColor = [UIColor redColor];
[self.view addSubview:_progressView];
}
return _progressView;
}
1刀诬、監(jiān)聽屬性
//添加監(jiān)聽
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
2究抓、監(jiān)聽屬性變化執(zhí)行
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"estimatedProgress"]) {
self.progressView.progress = self.webView.estimatedProgress;
//加載完成
if (self.webView.estimatedProgress >=1.0) {
[UIView animateWithDuration:0.25 animations:^{
self.progressView.alpha = 0.0f;
self.progressView.progress = 0.0f;
}];
}else{
self.progressView.alpha = 1.0f;
}
}
}
查看效果:
修改前:
修改后:
適用方向:
可以除掉一些html界面的廣告蜘欲,或者自定義html中的的某些視圖益眉,在下修為尚淺,文章僅限學(xué)習(xí)或參考姥份,若有幫助到您郭脂,前點擊喜歡或收藏,謝謝澈歉!