Noi*_*art 5 react-native react-native-android
我已使用本机 fetch API 登录到我的应用程序。我现在需要加载一个图像,所以我将它加载到一个<WebView>. 是否可以告诉 webview 使用这些 cookie?
您可以使用 postMessage() 将包含 cookie 内容的消息发布到您的 Webview。
您需要获取 Web 视图的引用,然后将消息发布到您的 Web 视图。
let webviewRef = null;
const cookiesContent = 'My cookies bla bla bla'
render() {
return (
<WebView
onLoad={() => webviewRef.postMessage(cookiesContent)}
ref={webview => { webviewRef = webview; }}
source={{ uri: 'https://YourUrl' }}
/>
);
}
Run Code Online (Sandbox Code Playgroud)
然后在您的网站中您可以创建 cookie 并使用它
<script>
document.addEventListener("message", function(data) {
document.cookie=`cookiesName=${data.data}`;
});
</script>
Run Code Online (Sandbox Code Playgroud)
如果您不是网站的所有者,您仍然可以尝试使用 Webview 组件的injectedJavaScript props 来注入 javascript。
const JsCode = 'document.addEventListener("message", function(data) {
document.cookie=`cookiesName=${data.data}`;
});';
<WebView
injectedJavaScript={JsCode}
onLoad={() => webviewRef.postMessage(cookiesContent)}
ref={webview => { webviewRef = webview; }}
source={{ uri: 'https://YourUrl' }}
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2644 次 |
| 最近记录: |