I have a webview on a Pageview who display a chat from a plugin that I use on my wordpress website (I have no access to data from this plugin). It’s not a chat with FB or google account, it’s only an open chat room, where users can add and save her nickname (I suppose nickname is stored in cookies ?). As long as the webview is active the nickname remains memorized. Problem, after each time the app is close and reopen, the user lose his nickname.
Here is my code
WebView(
initialUrl: 'https://XXXX',
javascriptMode: JavascriptMode.unrestricted,
gestureRecognizers: [
Factory(() => PlatformViewVerticalGestureRecognizer()),
].toSet(),
),
How can I save session ? Even when after app is close and reopen ?
2
Answers
First, in your website project, add this javascript code which it will be accessible to the HTML pseodo input:
you can add it inside a
<script></script>
in the.html
file or in a.js
separate file.this basically will post a message with the pseudo input value to our app later.
Don’t forget to change inputSelectorHere with your psuedo input selector.
now in your flutter code, create a simple
Stirng
variable like this:then in the
WebView
widget:here the
JavascriptChannel
is set so it receives those messages which will be sent from your website from the webview, then it will be saved inside thecookie
variable which we created.when you close the webview and open it again, the
onWebViewCreated
will be called, and the cookie now is notnull
, so it will assign thecookie
we saved todocument.cookie
in the webview.As I can understand. You just need to get cookies (or cache and local storage) and store them in FlutterSecureStorage. when the user closes the app and re-opens just check if cookies are stored in FlutterSecureStorage or not.
If Cookies are present just add the cookies and refresh the page. I have written a pseudo code for the demo purpose (Code might not work as you expected but it will give you a brief idea about my approach).
I have added a code for the cookies. I also added code for the cache and local storage but you have to configure it according to your needs.
Please read the comments.