Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session not saving in ios flutter webview app

I have some issue with storing session details in my ios app. I created a webview app. Whenever i close the app after logging in, it logs me out and asks for credentials everytime. This is working fine for my android app. Any fix for this.

like image 834
Tushar Arora Avatar asked Aug 14 '26 23:08

Tushar Arora


1 Answers

You can store the session by calling below code:-

 onLoadStop: (controller, url) async {
        if (url == null) {
          return;
        }
        final cookieManager = CookieManager.instance();
        final expiresDate = DateTime.now()
            .add(const Duration(hours: 24))
            .millisecondsSinceEpoch;

        List<Cookie> cookieList = await cookieManager.getCookies(url: url);
        cookieList.where((cookie) => cookie.expiresDate == null).forEach(
              (cookie) => cookieManager.setCookie(
                url: url,
                name: cookie.name,
                value: cookie.value,
                path: cookie.path ?? '/',
                domain: cookie.domain,
                expiresDate: expiresDate,
                isSecure: cookie.isSecure,
                isHttpOnly: cookie.isHttpOnly,
                sameSite: cookie.sameSite,
              ),
            );
      }

You have to store session or cookies for web view using CookieManager and you can call this code when your url is loading is completed.

like image 59
Sanju Bhatt Avatar answered Aug 17 '26 14:08

Sanju Bhatt