WKWebView
cookies
iOS development
Swift
webview integration

Getting all cookies from WKWebView

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

swift
1import WebKit
2
3func printAllCookies(from webView: WKWebView) {
4    let cookieStore = webView.configuration.websiteDataStore.httpCookieStore
5
6    cookieStore.getAllCookies { cookies in
7        for cookie in cookies {
8            print("name: \(cookie.name)")
9            print("value: \(cookie.value)")
10            print("domain: \(cookie.domain)")
11            print("path: \(cookie.path)")
12            print("expires: \(String(describing: cookie.expiresDate))")
13        }
14    }
15}

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.

swift
1import WebKit
2
3let config = WKWebViewConfiguration()
4config.websiteDataStore = .nonPersistent()
5
6let webView = WKWebView(frame: .zero, configuration: config)

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:

swift
1import Foundation
2import WebKit
3
4func syncCookiesToSharedStorage(from webView: WKWebView) {
5    let cookieStore = webView.configuration.websiteDataStore.httpCookieStore
6
7    cookieStore.getAllCookies { cookies in
8        let sharedStorage = HTTPCookieStorage.shared
9        for cookie in cookies {
10            sharedStorage.setCookie(cookie)
11        }
12    }
13}

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:

swift
webView.evaluateJavaScript("document.cookie") { result, error in
    print(result ?? "")
}

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.getAllCookies to read all cookies from a WKWebView.
  • The API is asynchronous, so use the completion handler correctly.
  • Cookies belong to the specific WKWebsiteDataStore attached to the web view.
  • 'document.cookie only shows page-visible cookies, not the full store.'
  • Sync cookies explicitly if native networking code needs the same session state.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.