WKWebView 禁用 长按弹出UIMenuController

iOS8开始使用WKWebView,但实际使用过程中和UIWebView有许多区别。比如禁止在webview中长按弹出UIMenuController的操作:


  • UIWebView中
 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

  • WKWebView中,则需要通过js进行操作
// 禁止选择CSS
NSString *css = @"body{-webkit-user-select:none;-webkit-user-drag:none;}";

// CSS选中样式取消
NSMutableString *javascript = [NSMutableString string];
[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注入
WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:noneSelectScript];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;

// WKWebView 初始化
WKWebView *webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];

另外提一句,获取webView当前选中的内容方法:

NSString *highlighted = [_webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString();"];

参考链接:
http://blog.csdn.net/qq_30513483/article/details/51959187

http://www.cnblogs.com/sunshine-anycall/p/3245940.html

https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/AddingCustomEditMenuItems/AddingCustomEditMenuItems.html

http://notnull.hateblo.jp/entry/2015/05/15/134643

http://www.itdadao.com/articles/c15a265474p0.html

http://supereasyapps.com/blog/2014/4/17/show-the-uimenucontroller-and-display-custom-edit-menus-for-uiviewcontroller-uitableviewcontroller-and-uicollectionview-on-ios-7

http://www.jianshu.com/p/ddd59867909a

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值