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

Windows: getCookies return empty list #2314

Closed
2 tasks done
ascanime opened this issue Oct 1, 2024 · 7 comments
Closed
2 tasks done

Windows: getCookies return empty list #2314

ascanime opened this issue Oct 1, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@ascanime
Copy link

ascanime commented Oct 1, 2024

  • I have read the Getting Started section
  • I have already searched for the same problem

Environment

Technology Version
Flutter version 3.24.3
Plugin version 6.1.3
Android version
Windows version 11
iOS version
macOS version
Xcode version
Google Chrome version

Device information:

Description

I cannot retrieve the list of cookies for a site on my Windows machine

Expected behavior:
should return list of cookies

Current behavior:
return empty list

Steps to reproduce

final cookies = await CookieManager.instance()
          .getCookies(url: WebUri("https://sushiscan.net/"));
      print(cookies); // []
@ascanime ascanime added the bug Something isn't working label Oct 1, 2024
Copy link

github-actions bot commented Oct 1, 2024

👋 @ascanime

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Oct 1, 2024

Are you using a custom WebViewEnvironment for your WebView? In that case, you need to pass it also to the CookieManager.instance method

@ascanime
Copy link
Author

ascanime commented Oct 1, 2024

Yeah! I passed it but it always return empty list :

final cookies = await CookieManager.instance(
                           webViewEnvironment: webViewEnvironment)
                       .getCookies(url: WebUri("https://sushiscan.net"));

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Nov 11, 2024

it seems the problem is related to cookies with partition key.
The actual CookieManager implementation doesn't use the current WebView to get the cookies, so it could be useful implement the usage of the webview controller when using the cookie manager methods as it will solve the problem.

More Info about cookie with partition key: https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies

This means they can only be read within the context of the top-level site they were set on.

@pichillilorenzo
Copy link
Owner

The fix will be available in the next beta version 6.2.0-beta.2.

Here is an example of usage:

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

WebViewEnvironment? webViewEnvironment;

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (!kIsWeb && defaultTargetPlatform == TargetPlatform.windows) {
    final availableVersion = await WebViewEnvironment.getAvailableVersion();
    assert(availableVersion != null,
    'Failed to find an installed WebView2 Runtime or non-stable Microsoft Edge installation.');

    webViewEnvironment = await WebViewEnvironment.create(
        settings: WebViewEnvironmentSettings(userDataFolder: 'YOUR_CUSTOM_PATH'));
  }

  runApp(const MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewSettings settings = InAppWebViewSettings(
      isInspectable: kDebugMode,
  );
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text("sushiscan.net test")),
        body: SafeArea(
            child: Column(children: <Widget>[
              Expanded(
                child: Stack(
                  children: [
                    InAppWebView(
                      key: webViewKey,
                      webViewEnvironment: webViewEnvironment,
                      initialSettings: settings,
                      initialUrlRequest: URLRequest(url: WebUri("https://sushiscan.net/")),
                      onLoadStop: (controller, url) async {
                        // get cookie manager
                        final cookieManager = CookieManager.instance(
                            webViewEnvironment: webViewEnvironment);
                        // set also the webViewController to get all the possible cookies
                        print(await cookieManager.getCookies(url: url!, 
                            webViewController: controller));
                      },
                    ),
                  ],
                ),
              ),
            ])));
  }
}

Screenshot 2024-11-12 alle 02 03 16

@pichillilorenzo
Copy link
Owner

Released new version 6.2.0-beta.2 with the fix.

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants