-(WKWebView*)baseWkWebView
{
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
wkWebConfig.allowsInlineMediaPlayback = YES;
wkWebConfig.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
WKPreferences *preferences = [WKPreferences new];
preferences.javaScriptCanOpenWindowsAutomatically = YES;
wkWebConfig.preferences= preferences;
// var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}
// var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);
// 自适应屏幕宽度js
//NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
// NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
//禁止网页缩放
NSString *jSString = @"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = %f;"
"for(i=0;i"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = %f;"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
// 添加js调用
[wkUControlleraddUserScript:wkUserScript];
wkWebConfig.userContentController= wkUController;
if (!_baseWkWebView)
{
_baseWkWebView=[[WKWebView alloc]initWithFrame:CGRectMake(0, Height_NavBar, SCREEN_WIDTH, SCREEN_HEIGHT-Height_NavBar) configuration:wkWebConfig];
_baseWkWebView.scrollView.bounces = NO;
_baseWkWebView.opaque = NO;
// _baseWkWebView.UIDelegate = self;
// _baseWkWebView.navigationDelegate = self;
[_baseWkWebView sizeToFit];
}
return _baseWkWebView;
}
网友评论