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);
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 got this error:
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**.
How to set cookie that works on ios and android?
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.
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");')