Skip to content

fix(ios): retain WKHTTPCookieStore observer so cookie sync works - #8520

Open
Cybertron01Z wants to merge 1 commit into
ionic-team:mainfrom
Cybertron01Z:fix/ios-cookie-observer-strong-ref
Open

fix(ios): retain WKHTTPCookieStore observer so cookie sync works#8520
Cybertron01Z wants to merge 1 commit into
ionic-team:mainfrom
Cybertron01Z:fix/ios-cookie-observer-strong-ref

Conversation

@Cybertron01Z

@Cybertron01Z Cybertron01Z commented Jun 25, 2026

Copy link
Copy Markdown

Description

CAPBridgeViewController.webViewConfiguration(for:) added a CapacitorWKCookieObserver to the web view's httpCookieStore but did not keep any reference to it:

webViewConfiguration.websiteDataStore.httpCookieStore.add(CapacitorWKCookieObserver())

Per Apple's docs, WKHTTPCookieStore does not retain its observers:

The cookie store doesn't maintain a strong reference to the object you specify. You are responsible for removing your observer object before it becomes invalid.

So the observer was deallocated immediately after add(_:) returned, and cookiesDidChange(in:) never fired. The result: cookies set inside the WKWebView were never synced back to HTTPCookieStorage.

This PR holds a strong reference to the observer on the view controller, and also retains the store it was added to so the observer can be removed in deinit, as the docs require.

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

The cookie observer was dead on arrival — cookiesDidChange never ran because the instance had no owner and was released right away. WKWebView → HTTPCookieStorage cookie syncing has therefore been silently broken.

The observer is now stored in an optional property assigned at the call site, so a subclass that overrides webViewConfiguration(for:) without calling the observer code creates no dangling instance. A weak reference to the exact WKHTTPCookieStore is kept so deinit can remove the observer per Apple's contract.

Tests or Reproductions

Tested manually:

  • When the response sets a cookie, it is now successfully synced

Screenshots / Media

N/A

Platforms Affected

  • Android
  • iOS
  • Web

Notes / Comments

The default websiteDataStore is the .default() singleton, so the store reference stays valid for the controller's lifetime; the weak reference avoids extending the store's lifetime or creating a retain cycle.

cursor Bot pushed a commit to Cap-go/capacitor-plus that referenced this pull request Aug 13, 2026
…ieStore observer so cookie sync works (#83)

* Release 8.3.2

* fix(cli): copy plugin files in CocoaPods projects (ionic-team#8467)

* Release 8.3.3

* chore(cli): update telemetry endpoint (ionic-team#8464)

* Release 8.3.4

* chore: format java code (ionic-team#8475)

* chore: format java code (ionic-team#8477)

* feat: add method getDouble to plugin config (ionic-team#7638)

Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>

* fix(SystemBars): make `safe-area-inset-x` available on API <= 34 (ionic-team#8424)

* feat(cli): add experimental packageOptions (ionic-team#8471)

* feat(cli): capture ios_package_manager in telemetry (ionic-team#8482)

* fix(cli): revert live reload config on failure (ionic-team#8485)

* chore: fix typo in declarations.ts (ionic-team#8488)

* fix(SystemBars): respect `insetsHandling` disable (ionic-team#8481)

* fix(android): show only the requested system bar (ionic-team#8480)

Co-authored-by: Joey Pender <joey.pender@outsystems.com>
Co-authored-by: Pedro Bilro <pedro.gustavo.bilro@outsystems.com>
Co-authored-by: Mark Anderson <mark.anderson@outsystems.com>

* chore: Decrease timeout for CI jobs from 60 to 30 minutes (ionic-team#8476)

Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>

* Release 8.4.0

* fix(cli): patch Capacitor SPM dependency version in plugins (ionic-team#8492)

Co-authored-by: Mark Anderson <mark.anderson@outsystems.com>

* fix(cli): make SPM dependency patch work on prereleases (ionic-team#8508)

* Release 8.4.1

* chore: run `npm run fmt` to fix lint errors (ionic-team#8516)

* fix(ios): retain WKHTTPCookieStore observer so cookie sync works

* fix(ios): retain WKHTTPCookieStore observer so cookie sync works

(cherry picked from commit 3fc41b8)

---------

Co-authored-by: Github Workflow (on behalf of markemer) <users.noreply.github.com>
Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
Co-authored-by: Alex J. <alexandre.jacinto@outsystems.com>
Co-authored-by: Robin Genz <mail@robingenz.dev>
Co-authored-by: Joey Pender <joey.pender@outsystems.com>
Co-authored-by: Minh Vu <vuhoangminh97@gmail.com>
Co-authored-by: Pedro Bilro <pedro.gustavo.bilro@outsystems.com>
Co-authored-by: Mark Anderson <mark.anderson@outsystems.com>
Co-authored-by: Mika Ruch <mika.ruch@zuehlke.com>
Co-authored-by: Capacitor+ Bot <bot@capgo.app>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant