简体   繁体   中英

How to disable long press selection on WKWebView in iOS15?

With the iOS 15 update, in my web application is appear a new functionality that I want to disable. This feature is based on a long press and dragging is possible to select some text.

Feature example gif

特征示例 gif

i've tried to use:

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

but it disable all interactions with text, text inputs too.

Someone can help me, please?

Maybe just using CSS will solve your problem (write code below in your own html or css file):

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

If you are not familiar with CSS,here is a sample code that inject CSS with JS in your Objective-C Code:

[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];

Ref:

user-select - MDN

Inject CSS stylesheet as string using Javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM