WKWebView 屏蔽长按手势 - iOS

研究半天还跟正常套路不一样,WKWebView 需要将 JS 注入进去,套路啊 ... 

查半天资料,为了后者们开发可以提高效率,特此分享一下,不到的地方多多包涵哈.

废话不多说,直接上 code,将如下 code 直接 copy 进 WKWebView 控件实例化的地方即可.


//禁止长按弹出 UIMenuController 相关

//禁止选择 css 配置相关

NSString*css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";

//css 选中样式取消

NSMutableString*javascript = [NSMutableStringstring];

[javascript appendString:@"var style = document.createElement('style');"];

[javascript appendString:@"style.type = 'text/css';"];

[javascript appendFormat:@"var cssContent = document.createTextNode('%@');", css];

[javascript appendString:@"style.appendChild(cssContent);"];

[javascript appendString:@"document.body.appendChild(style);"];

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

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

//javascript 注入

WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript

injectionTime:WKUserScriptInjectionTimeAtDocumentEnd

forMainFrameOnly:YES];

WKUserContentController*userContentController = [[WKUserContentControlleralloc] init];

[userContentController addUserScript:noneSelectScript];

WKWebViewConfiguration*configuration = [[WKWebViewConfigurationalloc] init];

configuration.userContentController = userContentController;


//控件加载

[self.wkWebView.configuration.userContentControlleraddUserScript:noneSelectScript];


Over

如有什么不周的地方还请大家多多指点!

你可能感兴趣的:(WKWebView 屏蔽长按手势 - iOS)