wkWebView 的js交互
与js交互
/// 5 页面加载完成之后调用
-
(void)webView:(WKWebView )webView didFinishNavigation:(WKNavigation)navigation;
{// 直接调用js
[self.webViewevaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';"completionHandler:nil];
// 调用js参数
[self.webViewevaluateJavaScript:@"document.documentElement.style.webkitTouchCallout='none';"completionHandler:nil];
[self.showViewremoveFromSuperview];
NSString *jsToGetHTMLSource =@"document.getElementsByTagName('html')[0].innerHTML";
[self.activityViewstopAnimating];
// 调用js获取返回值
[self.webViewevaluateJavaScript:jsToGetHTMLSourcecompletionHandler:^(id_Nullable HTMLSource, NSError * _Nullable error) {
NSRange range = [HTMLSourcerangeOfString:@"Bad Gateway"];//判断字符串是否包含
bool urlIsTrue = (range.location ==NSNotFound);
if (urlIsTrue ==true ) {
range = [HTMLSource rangeOfString:@"Network is unreachable"];
urlIsTrue = (range.location ==NSNotFound);
}if (urlIsTrue ==true ) { range = [HTMLSource rangeOfString:@"页面不存在"]; urlIsTrue = (range.location ==NSNotFound); } if (urlIsTrue ==true) { range = [HTMLSource rangeOfString:@"网页无法访问"]; urlIsTrue = (range.location ==NSNotFound); } if (urlIsTrue ==true) { range = [HTMLSource rangeOfString:@"Not Found"]; urlIsTrue = (range.location ==NSNotFound); } //网页可访问隐藏我们自己的返回按钮 if (urlIsTrue==true) { self.topView.hidden =YES; } else { [webView stopLoading]; [selfaddShowView]; }
}];
[webView stopLoading];
}