Nagender Singh Nagender Singh - 10 days ago 16
iOS Question

How to get selection object from react-native webview

I have an app where I'm using react-native webview to display a document.

The user can select some text and perform some custom action that I provide.

How do I get the selection object from webview into my app?

Answer

From 0.37.0 onwards there is a new feature in RN that might help you. There is a new prop in the WebView: onMessage and also a new method, postMessage that enable two-way communication. From the WebView, you have to call window.postMessage, and it will invoke the function that you define as callback for onMessage. You can see a full example in the documentation. As for getting the selected text, check out this answer.