其它技术点IOS文章收集UIWebView和JS交互

IOS WKWebview 去掉被插入的广告

2016-11-01  本文已影响2815人  CoderFarmer

公司刚上架的app,活动当天因为web崩溃的bug邮件一大堆,就是这孙子啦


FF44A05D-530B-43B1-9EDE-78BA645822D9.png

原因就是:我们自己的web页不会有二级页面的 我也就没设置navigationDelegate,然后用户点击广告时就导致闪退了.
但是设置了代理,点击就跳转到他们的页面,妈蛋,怎能白给你打广告?还影响我们的用户体验.

就先拦截他们的跳转:
#pagram mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSString *hostname = navigationAction.request.URL.host.lowercaseString;
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated && ![hostname containsString:@"你们接口的域名"]) {
        decisionHandler(WKNavigationActionPolicyCancel);
    }else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

可是点击广告这孙子会提示加载失败....真是日了狗了
这就和他肛上了
百度搜WKWebview去广告 基本上没有,都是UIWebview的(难道我搜索的姿势不对?),那就能不能跟UIWebview一样的思路去搞?
但是问题又来了,网上UIWebview的那些方法基本上都是要先知道广告的class,可是这个广告是动态的:

D3F07D55-00C0-45FB-B398-7662DC8BC5A6.png

他们的ID是变化着的,这就不能写死JS了

先获取到整个页面htmlstring
#pagram mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    DefineWeakSelf;
    NSString *jsToGetHTMLSource = @"document.getElementsByTagName('html')[0].innerHTML";//获取整个页面的HTMLstring
    [webView evaluateJavaScript:jsToGetHTMLSource completionHandler:^(id _Nullable HTMLsource, NSError * _Nullable error) {
        MyLog(@"%@",HTMLsource);
        if (!error && NOEmptyStr(HTMLsource)) {
            [weakSelf removeADByHTMLSource:HTMLsource];
        }
     }];
}
取出那个ID

从上图中的白色框内可以看到,他提供了关闭广告的JS,我们可以根据这个取出ID 这个最好写个NSString的Category

/**
 隐藏网页中的广告

 @param HTMLSource 网页html源码串
 */
- (void)removeADByHTMLSource:(NSString *)HTMLSource {
    NSString *fromString = @"javascript:document.getElementById";
    NSString *toString = @".style.display='none'";
    NSArray *strArr = [HTMLSource componentsSeparatedFromString:fromString toString:toString];// (这个截取的头和尾自己看着截,自己怎么觉得方便怎么来)
    MyLog(@"%@====%@",strArr,strArr.firstObject);
    if (strArr.count) {
        NSString *removeAD_JS_Str = [NSString stringWithFormat:@"%@%@%@",fromString,strArr.firstObject,toString];//再拼接上
        [self.webView evaluateJavaScript:removeAD_JS_Str completionHandler:nil];//执行隐藏广告的JS
    }
}
Category

.h

@interface NSString (subString)
- (NSArray *)componentsSeparatedFromString:(NSString *)fromString toString:(NSString *)toString;
@end

.m

@implementation NSString (subString)
- (NSArray *)componentsSeparatedFromString:(NSString *)fromString toString:(NSString *)toString
{
    if (IsEmptyStr(fromString) || IsEmptyStr(toString)) {
        return nil;
    }
    NSMutableArray *subStringsArray = [[NSMutableArray alloc] init];
    NSString *tempString = self;
    NSRange range = [tempString rangeOfString:fromString];
    while (range.location != NSNotFound) {
        tempString = [tempString substringFromIndex:(range.location + range.length)];
        range = [tempString rangeOfString:toString];
        if (range.location != NSNotFound) {
            [subStringsArray addObject:[tempString substringToIndex:range.location]];
            range = [tempString rangeOfString:fromString];
        }
        else
        {
            break;
        }
    }
    return subStringsArray;
}
@end
这是用到的宏
/**
 *  判断是否是空字符串 非空字符串 = yes
 *
 *  @param string
 *
 *  @return
 */

#define  NOEmptyStr(string)  string == nil || string == NULL ||[string isKindOfClass:[NSNull class]] || [string isEqualToString: @""]  ||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0 ? NO : YES

/**
 *  判断是否是空字符串 空字符串 = yes
 *
 *  @param string
 *
 *  @return
 */
#define  IsEmptyStr(string) string == nil || string == NULL ||[string isKindOfClass:[NSNull class]]|| [string isEqualToString:@""] ||[[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0 ? YES : NO

结语

我还是觉得不严谨,不能应对所有的广告,有没有大神有更好的办法,求分享!
这个demo就不传啦,会暴露我公司的前端代码啦~~~~

上一篇下一篇

猜你喜欢

热点阅读