Flutter Webview plugin doesn't save session cookies on iOS?

5,507

To achieve the same results as on Android on an iOS Platform you have to make sure that your WebViews are created from the same WebView pool. Unfortunately, the current version of the FlutterWebViewPlugin does not support such behavior.

You can use this Fork instead. Just be clear: Using will cause all your WebViews to created from the same pool. If that's not an issue, go ahead.

Share:
5,507
InternetJohnny
Author by

InternetJohnny

Updated on December 17, 2022

Comments

  • InternetJohnny
    InternetJohnny over 1 year

    I am developing a simple app on Android Studio using Flutter and the flutter_webview_plugin.

    The starting url is a login page. After logging in, I close the app and then reopen it. Upon re-opening, I am still logged in on Android, but on iOS I am shown the login page again.

    How can I preserve the session cookies?

    I have already set clearCookies: false, when instantiating the plugin. I have seen some mentions here about NSHTTPCookieStorage on iOS, but am not sure if this is the key to fixing my problem.

    Here is my code:

    import 'package:flutter/material.dart';
    import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
    
    class WebView extends StatelessWidget {
      final String selectedUrl = "https://servicedesk.roff.pt/suporte";
    
      @override
      Widget build(BuildContext context) {
        return new SafeArea(
          left: false,
          top: true,
          right: false,
          bottom: false,
          child: WebviewScaffold(
            url: selectedUrl,
            withZoom: true,
            withLocalStorage: true,
            clearCookies: false,
            hidden: true,
            initialChild: Container(
              color: Colors.black,
              child: const Center(
                child: Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Image(
                    image: AssetImage('lib/assets/splash.png'),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    
    

    All help is appreciated

  • InternetJohnny
    InternetJohnny about 4 years
    I tried it but this did not solve the issue on iOS. If I login on my website and then close the app, when I reopen it I have to login again. This does not happen on Android (the session is preserved). Any suggestions?
  • J T
    J T about 4 years
    In my case the solution was to use the flutter_secure_storage plugin to save the credentials. In my use-case it was better to do a new login each time I open the app(even on android) as that way you refresh your WebView's session which prevents token-invalidation.
  • InternetJohnny
    InternetJohnny about 4 years
    Thanks, this is exactly what I ended up going for. Built myself a login screen, the user is able to store the credentials if they want, and then I use evalJavascript from flutter_webview_plugin to automatically log them in. The flutter_secure_storage plugin is gonna work like a charm
  • Cyrus the Great
    Cyrus the Great over 3 years
    What is WebView pool and how can i set cookies with this fork: pub.dev/packages/flutter_webview_plugin @JTLtManBearPig