公司剛上架的app,活動當(dāng)天因為web崩潰的bug郵件一大堆,就是這孫子啦
原因就是:我們自己的web頁不會有二級頁面的 我也就沒設(shè)置navigationDelegate,然后用戶點擊廣告時就導(dǎo)致閃退了.
但是設(shè)置了代理,點擊就跳轉(zhuǎn)到他們的頁面,媽蛋,怎能白給你打廣告?還影響我們的用戶體驗.
就先攔截他們的跳轉(zhuǎn):
#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一樣的思路去搞?
但是問題又來了,網(wǎng)上UIWebview的那些方法基本上都是要先知道廣告的class,可是這個廣告是動態(tài)的:
他們的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
從上圖中的白色框內(nèi)可以看到,他提供了關(guān)閉廣告的JS,我們可以根據(jù)這個取出ID 這個最好寫個NSString的Category
/**
隱藏網(wǎng)頁中的廣告
@param HTMLSource 網(wǎng)頁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];//執(zhí)行隱藏廣告的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
結(jié)語
我還是覺得不嚴(yán)謹(jǐn),不能應(yīng)對所有的廣告,有沒有大神有更好的辦法,求分享!
這個demo就不傳啦,會暴露我公司的前端代碼啦~~~~