iOS中 WKWebview有效实现禁止滑动,禁止双指缩放,禁止

2019-07-17  本文已影响0人  轻重缓急

WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.preferences = [WKPreferences new];
config.preferences.minimumFontSize = 10;
config.preferences.javaScriptEnabled = YES; config.preferences.javaScriptCanOpenWindowsAutomatically = YES;

//下方代码,禁止缩放
WKUserContentController *userController = [WKUserContentController new];
NSString *js = @" ('meta[name=description]').remove();('head').append( '<meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">' );";
WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO]; NSMutableString *javascript = [NSMutableString string];
[javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];

//禁止长按
[javascript appendString:@"document.documentElement.style.webkitUserSelect='none';"];

//禁止选择
WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[userController addUserScript:script];
[userController addUserScript:noneSelectScript];
[userController addScriptMessageHandler:self name:@"openInfo"];
config.userContentController = userController;
_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
_webView.scrollView.bounces = false;//禁止滑动

上一篇下一篇

猜你喜欢

热点阅读