如何以编程方式禁用文本选择 WKWebView?

问题描述 投票:0回答:2

问题

您可以在WKWebView中选择文本。

期望的行为

您根本不能在 webview 中选择文本。

我试过的

  1. 加载
    NSString * jsCallBack = @"window.getSelection().removeAllRanges();";
    使用
    evaluateJavaScript:completionHandler:
  2. 使用
    "document.documentElement.style.webkitUserSelect='none'"
  3. 执行上述操作
  4. webView.configuration.selectionGranularity = nil;
    <-- This one doesn't make sense as
    selectionGranularity
    只能取两个预定义的值,但值得一试。
  5. 试图在故事板中寻找解决方案,找不到不会禁用用户交互的解决方案。

我不能做什么

更改 HTML/CSS 代码目前不是一个选项,如果我可以更改它这个答案可能会工作。

ios objective-c wkwebview
2个回答
0
投票

半私人灰色区域 解决方案依赖于私人网络视图层次结构及其点击手势。作为奖励,它不依赖于私有类和方法符号,但如果点击手势的顺序发生变化,它就会中断。

@import WebKit;
@interface ViewController ()

@property(null_resettable, nonatomic,strong) WKWebView *view;
@end

@implementation ViewController

@dynamic view;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view = [WKWebView new];

    for (UIView *view in self.view.subviews) {
        for (UIView *innerView in view.subviews) {
            if (innerView.gestureRecognizers.count > 0) {
                NSMutableArray<UIGestureRecognizer*> *gestureRecognizers = [innerView.gestureRecognizers mutableCopy];
                [gestureRecognizers removeObjectAtIndex:0];
                innerView.gestureRecognizers = [gestureRecognizers copy];
            }
        }
    }

    [self.view loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://stackoverflow.com/questions/43227437/how-to-disable-text-selection-wkwebview-programatically"]]];
}
@end

0
投票
**// add gesture on viewDidLoad**
override func viewDidLoad() 
{
    super.viewDidLoad()
    let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: nil, action: nil)
    longPress.minimumPressDuration = 0.3
    webView.addGestureRecognizer(longPress)
}

**//also inherit the UIGestureRecognizerDelegate**

class ViewController: UIViewController, UIGestureRecognizerDelegate {
}
© www.soinside.com 2019 - 2024. All rights reserved.