-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[webview_flutter_platform_interface] Add support for getting cookie #11037
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||
|
||||||
| /// Gets a list of existing cookie for specified domain from all | |
| /// Returns a list of existing cookies for the specified domain from all |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
}
}
}
}
}
🍪 Total cookies in shared store: 2
Name: webview1 Value: value1
Name: webview2 Value: value2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
- The style guide states that code should be tested. (link)
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.