Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[webview_flutter_platform_interface] Add support for getting cookie
  • Loading branch information
khaled-0 committed Feb 17, 2026
commit 54cbe1160f6cb1fa22b703916268d70c46f50e20
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.14.1

* Adds support to retrieve WebView cookies. See `PlatformWebViewCookieManager.getCookies`.
* Updates minimum supported SDK version to Flutter 3.32/Dart 3.8.

## 2.14.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ abstract class PlatformWebViewCookieManager extends PlatformInterface {
'setCookie is not implemented on the current platform',
);
}

/// Gets a list of existing cookie for specified domain from all
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
/// Gets a list of existing cookie for specified domain from all
/// Returns a list of existing cookies for the specified domain from all

/// [WebView] instances of the application.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part true? iOS docs suggests that it only gets cookies of the associated webview: https://developer.apple.com/documentation/webkit/wkhttpcookiestore/getallcookies(_:)#Discussion

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be true
I ran a test (with Ai)

import SwiftUI
import UIKit
import WebKit

@main
struct testApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

// MARK: - WebView Representable
struct WebView: UIViewRepresentable {
    let url: URL
    let store: WKWebsiteDataStore

    func makeUIView(context: Context) -> WKWebView {
        let config = WKWebViewConfiguration()
        config.websiteDataStore = store
        let webView = WKWebView(frame: .zero, configuration: config)
        webView.navigationDelegate = context.coordinator
        webView.load(URLRequest(url: url))
        return webView
    }

    func updateUIView(_ uiView: WKWebView, context: Context) {}

    func makeCoordinator() -> Coordinator {
        Coordinator()
    }

    class Coordinator: NSObject, WKNavigationDelegate {}
}

// MARK: - ContentView
struct ContentView: View {
    let store = WKWebsiteDataStore.default()

    var body: some View {
        VStack {
            WebView(
                url: URL(
                    string: "https://httpbin.org/cookies/set?webview1=value1"
                )!,
                store: store
            )
            .frame(height: 200)
            .border(Color.blue)

            WebView(
                url: URL(
                    string: "https://httpbin.org/cookies/set?webview2=value2"
                )!,
                store: store
            )
            .frame(height: 200)
            .border(Color.green)

            Button("Print Cookies") {
                store.httpCookieStore.getAllCookies { cookies in
                    print("🍪 Total cookies in shared store:", cookies.count)
                    for cookie in cookies {
                        print("Name:", cookie.name, "Value:", cookie.value)
                    }
                }
            }
            .padding()
        }
        .padding()
    }
}

class ViewController: UIViewController, WKNavigationDelegate {

    var webView1: WKWebView!
    var webView2: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Shared persistent store
        let store = WKWebsiteDataStore.default()

        // First WebView
        let config1 = WKWebViewConfiguration()
        config1.websiteDataStore = store
        webView1 = WKWebView(
            frame: CGRect(x: 0, y: 50, width: view.bounds.width, height: 200),
            configuration: config1
        )
        webView1.navigationDelegate = self
        view.addSubview(webView1)

        // Second WebView
        let config2 = WKWebViewConfiguration()
        config2.websiteDataStore = store
        webView2 = WKWebView(
            frame: CGRect(x: 0, y: 300, width: view.bounds.width, height: 200),
            configuration: config2
        )
        webView2.navigationDelegate = self
        view.addSubview(webView2)

        // Load pages that set cookies
        if let url1 = URL(
            string: "https://httpbin.org/cookies/set?webview1=value1"
        ) {
            webView1.load(URLRequest(url: url1))
        }
        if let url2 = URL(
            string: "https://httpbin.org/cookies/set?webview2=value2"
        ) {
            webView2.load(URLRequest(url: url2))
        }
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        let store = WKWebsiteDataStore.default().httpCookieStore
        store.getAllCookies { cookies in
            DispatchQueue.main.async {
                print("🍪 Total cookies in shared store:", cookies.count)
                for cookie in cookies {
                    print("Name:", cookie.name, "Value:", cookie.value)
                }
            }
        }
    }
}

Simulator Screenshot - iPad (A16) - 2026-03-01 at 11 07 45
🍪 Total cookies in shared store: 2
Name: webview1 Value: value1
Name: webview2 Value: value2

Future<List<WebViewCookie>> getCookies(Uri url) {
throw UnimplementedError(
'getCookies is not implemented on the current platform',
);
}
Comment on lines +70 to +74

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The repository's contribution guide requires that code changes are tested. Please add a unit test for the new getCookies method to verify that its default implementation throws an UnimplementedError.

References
  1. The style guide states that code should be tested. (link)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this in the original PR, but this is actually missing a test. Though it looks like test/platform_webview_cookie_manager_test.dart was never created, so you will need to create a new test file. You can follow the pattern of test/platform_webview_controller_test.dart.

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,9 @@ class WebViewCookie {
/// Its value should match "path-value" in RFC6265bis:
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-02#section-4.1.1
final String path;

@override
String toString() {
return 'WebViewCookie{name: $name, value: $value, domain: $domain, path: $path}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.14.0
version: 2.14.1

environment:
sdk: ^3.8.0
Expand Down