Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On the Android platform, it's impossible to obtain cookies via CookieManager in some lower - version systems. #2518

Open
1 of 2 tasks
lifei opened this issue Jan 29, 2025 · 5 comments
Labels
bug Something isn't working

Comments

@lifei
Copy link

lifei commented Jan 29, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I've encountered a very strange phenomenon. With the same code and the same APK package, there are significant differences in behavior between the emulator with API 28, the emulator with API 34, and the real device (Android 14). In short, the CookieManager.instance().getCookies function fails to retrieve the WebView cookies on the API 28 emulator, but there is no such problem on the API 34 emulator and the real device.

Expected Behavior

The expectation is to be able to obtain cookies through code, just like on a real device.

Steps with code example to reproduce

Steps with code example to reproduce
final iaw.CookieManager cookieManager = iaw.CookieManager.instance();
final cookieJar = CookieJar();
final dio = Dio();
const baseUrl = "";
const cookieDomain = "";

Future InitDio() async {
  dio.interceptors.add(dcm.CookieManager(cookieJar));
  await UpdateCookie();
}

Future UpdateCookie({iaw.InAppWebViewController? webController = null}) async {
  var cookies = await cookieManager.getCookies(
      url: iaw.WebUri(cookieDomain), webViewController: webController);
  log("cookies count: ${cookies.length}");
  await cookieJar.saveFromResponse(
      Uri.parse(cookieDomain),
      cookies.map((cookie) {
        var c = new Cookie(cookie.name, cookie.value);
        c.path = cookie.path;
        c.domain = cookie.domain;
        c.sameSite = SameSite.lax;
        c.secure = cookie.isSecure ?? true;
        c.maxAge = cookie.expiresDate;
        c.httpOnly = cookie.isHttpOnly ?? false;
        log("update cookie: ${cookie.name}");
        return c;
      }).toList());
}

Stacktrace/Logs

Stacktrace/Logs
cookies count: 0

Flutter version

v3.27.3

Operating System, Device-specific and/or Tool

  • Android emulator API 28 ❌
  • Android emulator API 34 ✔
  • XIaomi Mix Fold 3 Android Version 14 ✔

Plugin version

v6.1.5, v6.2.0-beta.1

Additional information

No response

Self grab

  • I'm ready to work on this issue!
@lifei lifei added the bug Something isn't working label Jan 29, 2025
@lifei
Copy link
Author

lifei commented Jan 29, 2025

I have done some research on this issue and found that in a good scenario it prints here1 and in a bad scenario it prints here2.

  public List<Map<String, Object>> getCookies(final String url) {

    final List<Map<String, Object>> cookieListMap = new ArrayList<>();

    cookieManager = getCookieManager();
    if (cookieManager == null) return cookieListMap;

    List<String> cookies = new ArrayList<>();
    if (WebViewFeature.isFeatureSupported(WebViewFeature.GET_COOKIE_INFO)) {
      Log.i(LOG_TAG, "here1");
      cookies = CookieManagerCompat.getCookieInfo(cookieManager, url);
    } else {
      Log.i(LOG_TAG, "here2");
      String cookiesString = cookieManager.getCookie(url);
      if (cookiesString != null) {
        cookies = Arrays.asList(cookiesString.split(";"));
      }
    }

@lifei
Copy link
Author

lifei commented Jan 29, 2025

#2392 maybe this issue is the same problem.

@lifei
Copy link
Author

lifei commented Jan 29, 2025

I tried flutter_webview and I failed.

@lifei
Copy link
Author

lifei commented Jan 29, 2025

Finally, I found that SameSite:Lax is the key reason, even I passed controller to getCookie method.

@lifei
Copy link
Author

lifei commented Jan 29, 2025

Well, I find a link https://stackoverflow.com/questions/65812748/cookiemanager-getcookie-always-returns-null-version-api-28-pie, it said that there was a bug in google 28 image, https://bugs.chromium.org/p/chromium/issues/detail?id=780491.
then I tested it in google 29 image, and I worked fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant