WebView抖动:初始化URL时无法将'cookie'设置为标头 [英] WebView flutter: Failed to set the 'cookie' into header when initialize url
问题描述
我正在使用 webview_flutter:^ 1.0.7
在我的应用程序上显示网页:
I am using webview_flutter: ^1.0.7
to shows web page on my application:
WebView(
initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy.always_allow,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (webViewController) async {
_controller.complete(webViewController);
在initState方法中,我有以下代码:
in initState method i have these codes:
@override
void initState() {
super.initState();
cookieManagerr.clearCookies();
_controller.future.then((controller) async {
wvc = controller;
// wvc.evaluateJavascript('document.cookie = "SESSION-Test=token";');
Map<String, String> header = {'Cookie': 'ASP.NET_SessionId=222'};
wvc.loadUrl(
'${widget.url}?BranchName=&latitude=${latitude}&longitude=${longitude}',
headers: header,
);
});
}
但运行后未向标题添加任何内容.
but after running nothing is added to header.
如果我将代码更改为此:
If i change my code to this:
WebView(
initialMediaPlaybackPolicy:
AutoMediaPlaybackPolicy.always_allow,
initialUrl:
'${widget.url}?BranchName=&latitude=${latitude}&longitude=${longitude}',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (webViewController) async {
webViewController.evaluateJavascript(
'document.cookie = "SESSION-Test=token";');
我收到此错误:
I/chromium( 6239): [INFO:CONSOLE(1)] "Uncaught SecurityError: Failed to set the 'cookie' property on 'Document': Access is denied for this document.", source: (1)
D/EGL_emulation( 6239): eglMakeCurrent: 0xd2aea420: ver 2 0 (tinfo 0xb48e41f0)
I/chromium( 6239): [INFO:CONSOLE(17932)] "You are using OSRM's demo server. Please note that it is **NOT SUITABLE FOR PRODUCTION USE**.
如何设置可在ios和android上运行的cookie?
How to set cookie that works on ios and android?
推荐答案
首先,也许您的html有问题.例如, https://github.com/js-cookie/js-cookie/issues/531 说,如果脚本是在具有沙箱属性的iframe中执行的,则可能会引发错误.
Firstly, maybe there is problem with your html. For example, https://github.com/js-cookie/js-cookie/issues/531 says that if the script is executed inside an iframe with a sandbox attribute, it can throw an error.
第二,如何尝试使用另一种方式代替cookie?例如,要将一些变量从flutter传递到javascript,您可以:
Secondly, what about trying to use another way instead of cookies? For example, to pass some variables to javascript from flutter, you can:
-
webViewController.evaluateJavascript('window.hello ='world';;')
- 或者,您可以
webViewController.evaluateJavascript('window.your_js_method("world");')
这篇关于WebView抖动:初始化URL时无法将'cookie'设置为标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!