作者:FQQA
鏈接:http://www.itdecent.cn/p/42d818e85805
來源:簡書
簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。
有些圖片沒有上傳成功,就懶得管了,有需要的可以到上面的原創(chuàng)作者那里去看,這里只是個人備份_。
項目地址在文章最后
在做一個項目,需要對網(wǎng)站做解析,就是把網(wǎng)頁的不需要的部分去除。比如廣告啊,導(dǎo)航欄,登陸欄之類的。一般的做法就是
NSURL *url = [NSURL URLWithString:_link];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
然后,在- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation這個函數(shù)內(nèi)用js把想要去掉的節(jié)點刪掉。那么問題來了,webview加載網(wǎng)頁時是把已經(jīng)加載好的內(nèi)容先顯示出來,再繼續(xù)加載,直到所有的內(nèi)容都加載好了,- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation這個函數(shù)才會調(diào)用。所以你會看到一個完整的網(wǎng)頁,再一閃,才是你想要的網(wǎng)頁。當(dāng)然你也可以先隱藏網(wǎng)頁,等完成后再顯示。那么問題又來了,如果這個網(wǎng)頁要等很久才完成呢?剛好我的網(wǎng)頁竟然要等5分鐘才能完成,實在受不了。后來我想到先把html給弄來,對html進(jìn)行處理后再加載,發(fā)現(xiàn)這樣只要3,4秒。我的做法是
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_htmlString = [NSString stringWithContentsOfURL:_URLs[_index] encoding:NSUTF8StringEncoding error:nil];
if(_htmlString == nil || _htmlString.length == 0){
NSLog(@"load failed!");
}else{
_htmlString = [self filterHtmlString:_htmlString];
[_webView loadHTMLString:_htmlString baseURL:_URLs[_index]];
}
});
[_webView loadHTMLString:_htmlString baseURL:[NSURL URLWithString:_link]];這句話的baseURL不能為空,不然網(wǎng)頁就沒有背景啊,顏色那些資源。
我們對這個網(wǎng)站來處理一下
http://elderscrollsonline.wiki.fextralife.com
先去掉登錄注冊那部分,通過元素檢查,
如果你熟悉NSScanner,那就十分簡單
- (NSString *)filterHtmlString:(NSString *)htmlString{
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:htmlString];
[theScanner scanUpToString:@"<div id=\"fex-account\">" intoString:NULL];
[theScanner scanUpToString:@"</form>" intoString:&text];
htmlString = [htmlString stringByReplacingOccurrencesOfString:text withString:@""];
return htmlString;
}
text就是<div id="fex-account">···</div>那部分,替換成空字符,這樣頂部的登錄注冊就沒有了,幾秒的事情。
這里只是做了個簡單的示范,你可以刪除任何你看不順眼的。
但是還是有問題,webview原來的前進(jìn)后退功能就失去了,而且當(dāng)你點擊了當(dāng)前頁面的一個鏈接,新的頁面依然是用loadRequest:[NSURLRequest requestWithURL:url]的方式加載,又回到了原點。解決這兩個問題也就是說你要自己封裝一個webViewController。
首先要一個數(shù)組保存網(wǎng)址列表和一個下標(biāo)
_link = @"http://elderscrollsonline.wiki.fextralife.com";
_URLs = [NSMutableArray arrayWithObject:[NSURL URLWithString:_link]];
_index = 0;
下面是導(dǎo)航欄的點擊事件
-(void)click:(UIButton *)Btn{
switch (Btn.tag) {
case 1:{
//刷新
[_webView loadHTMLString:_htmlString baseURL:_URLs[_index]];
}
break;
case 0:{
//后退
if(_index > 0){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_htmlString = [NSString stringWithContentsOfURL:_URLs[_index - 1] encoding:NSUTF8StringEncoding error:nil];
if(_htmlString == nil || _htmlString.length == 0){
NSLog(@"load failed!");
}else{
_index--;
_htmlString = [self filterHtmlString:_htmlString];
[_webView loadHTMLString:_htmlString baseURL:_URLs[_index]];
}
});
}else{
[self.navigationController popViewControllerAnimated:YES];
}
}
break;
case 2:{
//前進(jìn)
if(_index < _URLs.count - 1){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_htmlString = [NSString stringWithContentsOfURL:_URLs[_index + 1] encoding:NSUTF8StringEncoding error:nil];
if(_htmlString == nil || _htmlString.length == 0){
NSLog(@"load failed!");
}else{
_index++;
_htmlString = [self filterHtmlString:_htmlString];
[_webView loadHTMLString:_htmlString baseURL:_URLs[_index]];
}
});
}
}
break;
default:
break;
}
}
最后實現(xiàn)WkWebView的NavigationDelegate,使用我們的方法來加載網(wǎng)頁。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
在發(fā)送請求之前,決定是否跳轉(zhuǎn)。這里也可以攔截你不想要的請求。這里的_statu是一個枚舉類型,如果當(dāng)前的下標(biāo)是最后一個而且你點擊了一個鏈接,就是在數(shù)組后面添加新的網(wǎng)址。如果下表在中間,就要把后面的刪掉,再添加。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *URLString = navigationAction.request.URL.absoluteString;
NSLog(@"監(jiān)測到的WKWebView上的請求 %@",URLString);
WKNavigationActionPolicy actionPolicy;
if([URLString hasPrefix:_link]){
actionPolicy = WKNavigationActionPolicyAllow;
if(_index == _URLs.count - 1){
_statu = Add;//新增
}else{
_statu = Switch;//替換
}
}else{
actionPolicy = WKNavigationActionPolicyCancel;
}
// 這句是必須加上的,不然會異常
decisionHandler(actionPolicy);
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler;
在收到響應(yīng)后,決定是否跳轉(zhuǎn)。這里我們把所有的響應(yīng)統(tǒng)統(tǒng)拒絕,換成我們的方法loadHTMLString。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSURL *URL = navigationResponse.response.URL;
NSLog(@"響應(yīng)頭url->%@", URL.absoluteString);
WKNavigationResponsePolicy responsePolicy = WKNavigationResponsePolicyCancel;
// 這句是必須加上的,不然會異常
decisionHandler(responsePolicy);
if([URL.absoluteString hasPrefix:_link]){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_htmlString = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:nil];
if(_htmlString == nil || _htmlString.length == 0){
NSLog(@"load failed!");
}else{
_index++;
if(_statu == Switch){
NSMutableIndexSet *set = [[NSMutableIndexSet alloc]init];
for(NSUInteger i = _index; i < _URLs.count; i++){
[set addIndex:i];
}
[_URLs removeObjectsAtIndexes:set];
}
[_URLs addObject:URL];
_htmlString = [self filterHtmlString:_htmlString];
[_webView loadHTMLString:_htmlString baseURL:_URLs[_index]];
}
});
}
}
這樣就大功告成了。使用一段時間后雖然還沒有發(fā)現(xiàn)什么問題,但就是感覺不踏實啊。如果有哪里做得不好的歡迎大家指出討論。
完整的項目已放在Github上:https://github.com/FQQA/iOS
作者:FQQA
鏈接:http://www.itdecent.cn/p/42d818e85805
來源:簡書
簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。