1.最近项目上架苹果商店用到了webview,被拒了,原因如图:
截屏2020-05-1310.20.00.png其实在项目中是做了判断的,ios8以后用的wkwebview,而ios8以前采用webview,还是被拒,果断删除了这个判断,因为项目本身就已经要求了环境是ios9以上,然后重新提交,继续被拒,原因还是同样的原因,然后就搜整个项目的webview,在三方请求afnet里面有个webview的类别,继续删除这个类别,重新提交才ok
ps: afnet不是最新的,因为是pod进项目的,没有及时更新,其实更新一下一样ok
2.项目中用到webview的需求如下:
拿到地址url,但这个地址直接打开是有广告等一些不要展示在项目中的,所以就需要解析里面的标签对,获取想要展示的标签
项目中用的一个三方库hpple
先用wkwebview加载该url
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
// document.getElementsByTagName('img').style.maxWidth='100%';
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
_loadWebView=[[WKWebView alloc] initWithFrame:self.view.bounds configuration:wkWebConfig];
_loadWebView.backgroundColor = [UIColor whiteColor];
_loadWebView.navigationDelegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:_homeUrl];
[wkWebView loadRequest:request];
然后在代理方法中解析出想要的内容,直接上代码:
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
NSString *urlString=webView.URL.absoluteString;
if ([ webView.URL.absoluteString containsString:@"https://mini.eastday.com"]) {
if ([urlString hasPrefix:@"https://mini.eastday.com/mobile"]) {
NSString *last=[urlString componentsSeparatedByString:@"/mobile/"].lastObject;
urlString=[NSString stringWithFormat:@"https://mini.eastday.com/a/%@",last];
}
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];
TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data];
NSArray * elements = [doc searchWithXPathQuery:@"//div[@class='J-contain_detail_cnt contain_detail_cnt']"];
NSString *js=@"<script type=\"text/javascript\">window.onload= function(){\
var oImg = document.getElementsByTagName(\"img\");\
for(var i=0;i<oImg.length;i++){\
oImg[i].removeAttribute(\"height\");\
}\
}</script>";
NSString *head1=@"<html><head>";
NSString *head2=@"<title></title></head><style>img{max-width: 100% !important;}p{\"text-indent:2em;font-size: 30px;\"}</style><body>";
NSString *headStr=[NSString stringWithFormat:@"%@%@%@",head1,js,head2];
NSString *last=@"</body></html>";
for (int i = 0; i < elements.count; i++){
TFHppleElement *ulE = [elements objectAtIndex:i];
NSString *rrr=[ulE.raw stringByReplacingOccurrencesOfString:@"src=\"" withString:@"src=\"https:"];
NSString *uuu=[NSString stringWithFormat:@"%@%@%@",headStr,rrr,last];
[self.loadWebView loadHTMLString:uuu baseURL:nil];
[self.view addSubview:self.loadWebView];
}
}else{
}
}
网友评论