Getting all cookies from WKWebView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
With WKWebView, cookies do not live in the old shared storage model used by UIWebView. On modern iOS and macOS, the correct way to retrieve all cookies for a web view is through the web view’s websiteDataStore.httpCookieStore, which exposes an asynchronous API.
Use WKHTTPCookieStore on Modern Systems
If the web view uses the default or a custom WKWebsiteDataStore, get the cookies from that store directly:
This is the standard answer on iOS 11 and later, where WKHTTPCookieStore became the public cookie API for WebKit data stores.
The important detail is that getAllCookies is asynchronous. You do not get the cookie array back immediately, so any dependent work must happen inside the completion handler or be chained from it.
Make Sure You Read the Right Data Store
Cookies are tied to the web view’s data store. If you build the web view with a non-persistent store, its cookies are isolated from the default shared store.
If you later inspect a different store, you may conclude that cookies are “missing” when they are really stored elsewhere.
That is why the safest pattern is to always read cookies from webView.configuration.websiteDataStore, not from some unrelated global assumption.
Syncing Cookies to Network Code
Sometimes you want the WKWebView cookies for your own URLSession requests. In that case, copy them explicitly:
This is useful when the web view performs an authentication flow and the native app later needs the same session cookies.
What document.cookie Can and Cannot See
Some developers try to retrieve cookies by evaluating JavaScript:
That only exposes cookies visible to page JavaScript. It does not give you the full cookie store, and it cannot read HttpOnly cookies. Use this only when you specifically want the browser-visible cookie string, not when you need the authoritative full set of cookies.
Older Platform Constraints
Before WKHTTPCookieStore, cookie handling around WKWebView was much more awkward. If you are supporting very old OS versions, the clean public API may not exist, and the workaround options are more limited.
For current code, the main takeaway is simple: prefer WKHTTPCookieStore and design your web/native integration around that API.
Common Pitfalls
The biggest mistake is assuming HTTPCookieStorage.shared automatically contains everything used by WKWebView. That is not a safe assumption.
Another issue is forgetting that getAllCookies is asynchronous. Reading a variable immediately after calling it will often show an empty or stale result because the completion handler has not run yet.
Developers also sometimes read cookies from the wrong WKWebsiteDataStore, especially when mixing persistent and non-persistent web views.
Finally, do not confuse JavaScript-visible cookies with the full cookie store. document.cookie is not a replacement for getAllCookies.
Summary
- Use
webView.configuration.websiteDataStore.httpCookieStore.getAllCookiesto read all cookies from aWKWebView. - The API is asynchronous, so use the completion handler correctly.
- Cookies belong to the specific
WKWebsiteDataStoreattached to the web view. - '
document.cookieonly shows page-visible cookies, not the full store.' - Sync cookies explicitly if native networking code needs the same session state.
Related reading
- Getting and Setting Cursor Position of UITextField and UITextView in Swift
- Getting and Setting Cursor Position of UITextField and UITextView in Swift
- Getting current device language in iOS?
- Getting device orientation in Swift
- Getting error No such module using Xcode, but the framework is there
- Getting error No such module using Xcode, but the framework is there
- getting exception IllegalStateException Can not perform this action after onSaveInstanceState
- Getting “INTERNAL” exception when an async Firebase function is called from android app
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.