4

I'm trying to figure out how to disable text selection while in a React-Native WebView for my ios app. Using the css style user-select set to none on the site doesn't seem to be working while using a device or simulator but it does work in a browser. Is there a way to disable this using the React-Native WebView or do I need to use some native code to achieve this?

Using React-Native 0.30, iOS 9-10

5 Answers 5

2

I was able to disable zooming, text selection and other pointer events on the React Native side, by wrapping WebView in a View and setting a few props:

<View pointerEvents="none">
  <WebView
    source={{ uri: webviewUrl }}
    scrollEnabled={false}
  />
</View>
Sign up to request clarification or add additional context in comments.

1 Comment

After adding this, it is disabling all webview touches. Is there any way to disable only long press to avoid the copy-select-paste option?
1

Assuming that you are accessing a particular url on the webview in react native, then in the url, you can use the following tricks:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<div style="font-size:80%;padding-left:10px;padding-right:10px;">


<!DOCTYPE html>
<html>
<head>
  <script>
    function absorbEvent_(event) {
      var e = event || window.event;
      e.preventDefault && e.preventDefault();
      e.stopPropagation && e.stopPropagation();
      e.cancelBubble = true;
      e.returnValue = false;
      return false;
    }

    function preventLongPressMenu(node) {
      node.ontouchstart = absorbEvent_;
      node.ontouchmove = absorbEvent_;
      node.ontouchend = absorbEvent_;
      node.ontouchcancel = absorbEvent_;
    }

    function init() {
      preventLongPressMenu(document.getElementByTagName('body img'));
    }
  </script>
  <style>
    *:not(input):not(textarea) {
    -webkit-user-select: none; /* disable selection/Copy of UIWebView */
        -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */

    }
  </style>
</head>

<body onload="init()"  >


TEST<br>TEST<br>TEST<br>TEST<br>TEST<br>TEST<br>TEST<br>TEST<br>TEST<br>TEST<br>TEST<br>

</body>

The above will disable text selection, but it allows other functions such as "click". (it works in both iOS and android devices)

Enjoy...

Comments

0

The documentation does not mention this, so I would go with native implementation, which is easier than it might seem. You just have to re-implement this files (and maybe some others) in a native module that includes the options you want, see this answer and this one.

Maybe this package can also help you implement your module: https://github.com/lucasferreira/react-native-webview-android

Comments

0

I found a solution. Please try.

* {
  -webkit-user-select: none;
  -webkit-touch-callout: none; 
}

1 Comment

Can you explain what it does?
0

I found that add body {user-select: none} can resolve the issue.

body {user-select: none}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.