Skip to content

Cookies, Web Storage, and Credential Management

A WebView is a stateful browser: it keeps a cookie jar, two JavaScript storage areas, and a store of HTTP authentication credentials. This page documents the three Dart facades that expose that state — CookieManager, WebStorageManager (plus the per-controller WebStorage), and HttpAuthCredentialDatabase — how the per-WebView CookieController facade narrows the global cookie API to the current URL, and where each platform’s native cookie store actually lives. It closes with the settings that decide which store a WebView writes to (incognito, persistentStoreIdentifier, sharedCookiesEnabled, thirdPartyCookiesEnabled), because on Apple platforms the cookie manager’s target store is derived from those settings rather than configured separately.

The Three Facades and How They Reach the Platform

Section titled “The Three Facades and How They Reach the Platform”

All three managers follow the same federated shape: a thin app-facing class holds a platform field, every method is a PlatformInterface-verified abstract declaration in the platform interface package, and the concrete platform package supplies the channel implementation. CookieManager is the only one that serializes calls — it wraps every operation in a synchronized Lock, so concurrent cookie mutations from multiple isolates of Dart code are ordered rather than interleaved. HttpAuthCredentialDatabase and WebStorageManager are plain pass-throughs with cached singletons.

graph TD
subgraph App-facing package
CM["CookieManager<br/>(Lock-synchronized singleton)"]
CC["CookieController<br/>(per-WebView facade)"]
WSM["WebStorageManager"]
WS["WebStorage / LocalStorage / SessionStorage"]
HAD["HttpAuthCredentialDatabase"]
end
subgraph Platform interface
PCM["PlatformCookieManager"]
PCD["PlatformCookieDelegate"]
PWS["PlatformWebStorage + PlatformStorage"]
PWSM["PlatformWebStorageManager"]
PHAD["PlatformHttpAuthCredentialDatabase"]
end
subgraph Platform packages
A["Android (MethodChannel)"]
I["iOS / macOS (MethodChannel)"]
L["Linux (WebKitGTK / libsecret)"]
W["Windows (CDP over WebView2)"]
O["OHOS (ArkWeb)"]
end
CC --> CM
CM --> PCM
PCM --> PCD
WSM --> PWSM
WS --> PWS
HAD --> PHAD
PCM --> A
PCM --> I
PCM --> L
PCM --> W
PCM --> O

Sources: cookie_manager.dart, platform_cookie_manager.dart, http_auth_credentials_database.dart, web_storage_manager.dart

The PlatformCookieDelegate seam is the domain-controller split applied to cookies (issue #229, P3). The shared PlatformCookieManager stays the source of truth for global operations, while the delegate exposes only the per-controller subset — the operations that need a PlatformInAppWebViewController as context. Its default getter returns null, so a platform package that has not been migrated keeps compiling and simply has no delegate.

Sources: platform_cookie_delegate.dart, platform_inappwebview_controller.dart, android_cookie_delegate.dart

CookieManager.instance() returns a lazily created process-wide singleton. On Windows only, passing a webViewEnvironment bypasses the cache and constructs a manager bound to that specific WebView2 environment instead, which is how a non-default user-data folder gets its own jar.

Sources: cookie_manager.dart, platform_cookie_manager.dart

The webViewController parameter is not decorative. On iOS below 10.13, macOS below 10.13, and the Web platform, there is no native cookie API to call, so the manager falls back to JavaScript: setCookie becomes document.cookie="…" evaluated in that controller (or in a throwaway PlatformHeadlessInAppWebView when the controller is null or has JavaScript disabled), and getCookies reads document.cookie back. That fallback path cannot see HttpOnly cookies and returns only name and value populated.

Sources: platform_cookie_manager.dart, cookie_manager.dart, cookie_manager.dart

Method Scope Notes
setCookie One URL Replaces any cookie with same host/path/name; ignored if already expired
getCookies / getCookie One URL getCookie is a name filter over the same native query
deleteCookie / deleteCookies One URL (+ path/domain) deleteCookies removes every cookie at that path/domain
getAllCookies Whole store No controller context; global
deleteAllCookies Whole store Global
removeSessionCookies Whole store Reports whether anything was removed

Sources: cookie_manager.dart, platform_cookie_manager.dart

CookieController: Scoped to the Current WebView

Section titled “CookieController: Scoped to the Current WebView”

InAppWebViewController.cookies returns a lazily created CookieController. Its contract is small and precise: when you omit url, it resolves the WebView’s own current URL via getUrl() and passes the parent controller as context; when no URL is available it degrades gracefully ([], null, or false) instead of throwing. The three global operations — getAllCookies, deleteAllCookies, removeSessionCookies — are forwarded without URL resolution because they are store-wide by definition.

Sources: in_app_webview_controller.dart, cookie_controller.dart, cookie_controller.dart

The behavioral test suite pins this contract down: it asserts that the facade forwards identical arguments plus the platform controller as context, that an explicit URL wins over the current URL, and that the no-override path resolves CookieManager.instance() cleanly.

Sources: domain_controllers_cookie_behavioral_test.dart, fake_cookie_manager.dart

Web Storage: localStorage and sessionStorage

Section titled “Web Storage: localStorage and sessionStorage”

WebStorage is reached from the controller, not from a singleton: controller.webStorage exposes localStorage and sessionStorage, each a Storage subclass whose webStorageType is fixed at construction (LOCAL_STORAGE or SESSION_STORAGE). The wire value of that enum is the JavaScript identifier itself — 'localStorage' / 'sessionStorage' — which is what the platform implementations interpolate into their evaluated script.

Sources: web_storage.dart, web_storage.dart, web_storage_type.dart

The Android implementation is pure JavaScript evaluation against the controller: length, setItem, getItem, removeItem, clear, key, and a getItems() that walks the store with a for loop and returns key/value maps. Values are json.encoded on write and json.decoded on read, with a raw-string fallback when decoding fails. dispose() simply drops the controller reference.

Sources: web_storage.dart, web_storage.dart

WebStorageManager is the store-wide counterpart: getOrigins, deleteAllData, deleteOrigin, getQuotaForOrigin, getUsageForOrigin map to Android’s WebStorage class, while fetchDataRecords, removeDataFor, and removeDataModifiedSince map to WKWebsiteDataStore on iOS/macOS and are Apple-only. The WebsiteDataType enum carries the WKWebsiteDataType* wire strings verbatim, so records fetched on Apple platforms round-trip without a translation table. The AndroidWebStorageManager and IOSWebStorageManager fields on the manager are deprecated shims that forward to WebStorageManager.instance().

Sources: web_storage_manager.dart, platform_web_storage_manager.dart, website_data_type.dart, web_storage_manager.dart

HttpAuthCredentialDatabase manages username/password (and client-certificate) credentials keyed by a URLProtectionSpace — host, protocol, realm, port, authentication method, plus certificate and proxy metadata. Its six operations are getAllAuthCredentials, getHttpAuthCredentials, setHttpAuthCredential, removeHttpAuthCredential, removeHttpAuthCredentials, and clearAllAuthCredentials. Unlike the cookie manager, it takes no controller: credentials are keyed by protection space, not by WebView.

Sources: http_auth_credentials_database.dart, platform_http_auth_credentials_database.dart, url_protection_space.dart

The wire shape is a two-level entity: URLProtectionSpaceHttpAuthCredentials pairs one protection space with its list of URLCredential, and getAllAuthCredentials returns a list of those pairs. URLCredential carries username, password, optional client certificates, and a URLCredentialPersistence (NONE, FOR_SESSION, PERMANENT, SYNCHRONIZABLE).

Sources: url_protection_space_http_auth_credentials.dart, url_credential.dart, url_credential_persistence.dart

At runtime the challenge arrives through the onReceivedHttpAuthRequest event, which hands your handler an HttpAuthenticationChallenge (protection space, previousFailureCount, proposedCredential, and iOS-only failureResponse / error) and expects an HttpAuthResponse carrying username, password, permanentPersistence, and an HttpAuthResponseAction of CANCEL, PROCEED, or USE_SAVED_HTTP_AUTH_CREDENTIALS.

Sources: http_authentication_challenge.dart, http_auth_response.dart, http_auth_response_action.dart

Platform Cookie backend Credential backend Notable behavior
Android android.webkit.CookieManager via wtf.zikzak/zikzak_inappwebview_cookiemanager SQLite via wtf.zikzak/zikzak_inappwebview_credential_database Lazily initializes the Chromium task; flush() after mutations; uses CookieManagerCompat.getCookieInfo when GET_COOKIE_INFO is supported
iOS / macOS WKHTTPCookieStore URLCredentialStorage.shared Store is resolved per webViewId; JS fallback below iOS 10.13 / macOS 10.13
Linux WebKitGTK SoupCookie via wtf.zikzak/zikzak_inappwebview_cookiemanager libsecret (Secret Service D-Bus) maxAge is converted to an absolute expiresDate because native never reads it
Windows CDP Network.* / Storage.getCookies on WebView2 — Cookie selection stays inside WebView2; a shared environment controller serves global ops
OHOS ArkWeb via wtf.zikzak/arkweb_cookies — Dart-side domain/path matching and expiry-based deletion

Sources: MyCookieManager.java, MyCookieManager.java, MyCookieManager.swift, CredentialDatabase.swift, cookie_manager.cc, credential_database.h, cookie_manager.dart, cookie_manager.dart

Two platform details are worth internalizing. First, the Windows delete path compensates for CDP’s exact-domain matching: a cookie stored as .example.com will not be deleted by a request built from the bare host example.com, so _storedDomain reads the cookie back first and falls back to the host only when nothing matches. Second, the OHOS implementation reproduces RFC 6265 matching in Dart — leading-dot domain comparison, longest-path-first ordering, and secure-only-over-https filtering — because ArkWeb exposes only a whole-jar inventory.

Sources: cookie_manager.dart, cookie_manager.dart, cookie_manager.dart

Which Store Gets Used: The Settings That Matter

Section titled “Which Store Gets Used: The Settings That Matter”

Cookie isolation on Apple platforms is decided before the WKWebView exists, because websiteDataStore is immutable after initialization. Three settings interact: incognito selects WKWebsiteDataStore.nonPersistent() and wins over everything else; persistentStoreIdentifier (iOS 17+ / macOS 14+) selects a per-identifier persistent store so an account’s cookies survive relaunch in isolation from other accounts; otherwise cacheEnabled selects the shared .default() store. The identifier accepts a raw UUID, a 64-char SHA-256 hex string, or any stable string (hashed to a UUID).

Sources: InAppWebView.swift, in_app_webview_settings.dart

Because the store is chosen there, the cookie manager must resolve the same store when a webViewId is supplied. Both Apple implementations do exactly that: cookieStore(for:) looks the ID up in the headless WebView manager and returns that WebView’s configuration.websiteDataStore.httpCookieStore, falling back to WKWebsiteDataStore.default() for unscoped callers. This is the mechanism that keeps one account’s cookies out of another’s jar.

Sources: MyCookieManager.swift, MyCookieManager.swift

Two more settings shape cookie visibility rather than location. sharedCookiesEnabled (iOS 11+, default false) makes the WebView use HTTPCookieStorage.shared for every load request, which is what lets cookies written by the app’s networking stack reach the page. thirdPartyCookiesEnabled (Android, default true) maps to setAcceptThirdPartyCookies on the WebView instance and is re-applied on settings updates.

Sources: in_app_webview_settings.dart, in_app_webview_settings.dart, InAppWebView.swift, InAppWebView.java

Note that Android’s incognito is a different mechanism entirely: it calls CookieManager.getInstance().removeAllCookies(null) and disables caching, rather than swapping in an isolated store. Cookies set after that point still land in the process-wide jar.

Sources: InAppWebView.java

WebViewSessions is the clearest consumer: harvesting a session reads cookies through CookieManager.getCookies(url:) and localStorage through JavaScript evaluation, maps each plugin Cookie onto a portable CookieEntry field by field, and restoring clears the destination origin with deleteAllCookies() before replaying setCookie for each entry. The cookie manager is injectable so pure-Dart tests can run without a live WebView.

Sources: webview_sessions.dart, webview_sessions.dart, webview_sessions_test.dart

Continue with Disposal Lifecycle and KeepAlive Patterns to see how WebStorage.dispose(), the cookie manager singletons, and the credential database fit the package-wide disposal contract. For the store-isolation settings in depth — incognito, persistentStoreIdentifier, and per-account profiles — read Headless WebView and the Live-Instance Cap and Domain Controller Facades: Navigation, JavaScript, Cookies, Settings. For the native code behind the channel names listed above, see Android Native Layer (Java), Apple Native Layer (Swift and Swift Package Manager), Linux WPE WebKit Backend, and Windows WebView2 Facade and the DevTools Fork. Portable cookie-and-storage export is covered in Session Recording and Replay (Recipes), and the WebAuthn credential path that sits alongside HTTP auth is in WebAuthn and Passkey Bridge.