【问题标题】:How to disable long press selection on WKWebView in iOS15?如何在 iOS 15 中禁用 WKWebView 上的长按选择?
【发布时间】:2021-11-20 07:26:47
【问题描述】:

随着 iOS 15 的更新,在我的 Web 应用程序中出现了一个我想禁用的新功能。此功能基于长按和拖动可以选择一些文本。

特征示例 gif

我尝试过使用:

WKWebViewConfiguration* conf = [[WKWebViewConfiguration alloc] init];
WKPreferences *preferences = [[WKPreferences alloc] init];
preferences.textInteractionEnabled = false;
conf.preferences = preferences;

但它也会禁用与文本的所有交互,也禁用文本输入。

有人可以帮帮我吗?

【问题讨论】:

    标签: ios objective-c ios15


    【解决方案1】:

    也许仅仅使用 CSS 就能解决您的问题(在您自己的 html 或 css 文件中编写以下代码):

    user-select: none;
    -webkit-user-select: none;
    

    如果你不熟悉 CSS,这里有一个在你的 Objective-C 代码中注入 CSS 和 JS 的示例代码:

    [webView evaluateJavaScript:@"function addStyle(){const style=document.createElement('style');style.textContent='user-select: none;-webkit-user-select: none;';document.head.append(style)};addStyle();" completionHandler:NULL];
    

    参考:

    user-select - MDN

    Inject CSS stylesheet as string using Javascript

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 2023-01-12
      • 2017-01-12
      • 1970-01-01
      • 2021-12-16
      • 2021-09-28
      • 2020-09-27
      • 1970-01-01
      相关资源
      最近更新 更多