[英]how to send data from android application running on kotlin to a react js application which is opened on webview?
Suppose you are running an android application (kotlin) and u have logged in and have some data.假设您正在运行一个 android 应用程序 (kotlin) 并且您已登录并拥有一些数据。 Then in webview you open a react js application.
然后在 webview 中打开一个 react js 应用程序。 How can u pass that login data from android to react js application.
你如何从 android 传递登录数据来响应 js 应用程序。
I haven't done this personally, but I believe you would want to package any data you have inside of a script tag and inject it into the web view.我没有亲自做过这件事,但我相信您会想要将您拥有的任何数据打包到脚本标记中并将其注入到 Web 视图中。 For example, if the React application needs the value of
user_id
you would pass in the user id to a script like:例如,如果 React 应用程序需要
user_id
的值,您可以将用户 id 传递给如下脚本:
val script = """
<script> var user_id = ${user.id} </script>
"""
Because I'm not a React guy, that may not work right off the bat.因为我不是 React 人,所以这可能无法立即生效。 You might need to add the value to the
document
object and read it off that way.您可能需要将该值添加到
document
对象并以这种方式读取它。
Once the script is created then you can include it into your webview using the loadDataWithBaseURL
function .创建脚本后,您可以使用
loadDataWithBaseURL
函数将其包含到您的loadDataWithBaseURL
。 It would look something like this:它看起来像这样:
webView.loadDataWithBaseURL(reactAppUrl, script, "text/html", "utf-8", null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.