Mobile Development 11 min read

How WKWebView Manages Cookies: Storage Location & Sync Mechanics

This article explores the fundamentals of HTTP cookies within WKWebView, detailing how cookies are stored in memory or disk, the roles of UIProcess, WebContent, and NetworkProcess, and provides code examples for front‑end JavaScript, back‑end headers, and iOS client APIs.

Baidu App Technology
Baidu App Technology
Baidu App Technology
How WKWebView Manages Cookies: Storage Location & Sync Mechanics

1. Introduction

When a browser loads network resources it uses HTTP, a stateless client‑server protocol. Because HTTP is stateless, client‑side storage mechanisms such as cookies are used to keep state across requests.

2. Cookie Overview

According to MDN, an HTTP cookie is a small piece of data stored locally by the browser and sent with each request. Cookies are mainly used for:

Session state management : login status, shopping cart, game scores, etc.

Personalized settings : user‑defined preferences, themes, etc.

Browser behavior tracking : analyzing user behavior.

3. Basic Cookie Usage

3.1 Front‑end JavaScript

Reading and writing cookies follows the MDN syntax:

// Read all cookies accessible from the current page
allCookies = document.cookie;

// Write a new cookie
document.cookie = "someCookieName=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";

3.2 Backend Set‑Cookie Header

When the server returns a Set‑Cookie header the cookie can be inspected with tools such as Charles.

Set‑Cookie header example
Set‑Cookie header example

3.3 iOS Cookie API

iOS provides WKHTTPCookieStorage with the following methods:

@interface WKHTTPCookieStore : NSObject

- (void)getAllCookies:(void (^)(NSArray<NSHTTPCookie *> *))completionHandler;
- (void)setCookie:(NSHTTPCookie *)cookie completionHandler:(void (^ __nullable)(void))completionHandler;
- (void)deleteCookie:(NSHTTPCookie *)cookie completionHandler:(void (^ __nullable)(void))completionHandler;

@end

4. WebKit Cookie Technical Principles

4.1 Processes and Scenarios

WKWebView consists of three processes:

UIProcess : the app process. Calls WKHTTPCookieStorage which forwards operations to NetworkProcess via inter‑process communication.

WebContent : the renderer. Executes JavaScript cookie actions; each page keeps an in‑memory cookieCache synchronized with NetworkProcess.

NetworkProcess : the central cookie store backed by NSHTTPCookieStorage. It holds persistent and session cookies and broadcasts changes.

Three processes diagram
Three processes diagram

4.2 Coordination

Cookie coordination works as follows:

UIProcess does not store cookies directly; it forwards operations to NetworkProcess, which is the authoritative store.

WebContent registers listeners for cookie changes broadcast by NetworkProcess, keeping its cookieCache up‑to‑date.

When JavaScript calls setCookie, the string is parsed into an NSHTTPCookie, stored in cookieCache, then synchronized to NetworkProcess for persistent storage.

On cold start, NetworkProcess loads persisted cookies from disk into an in‑memory dictionary ( allCookies) and notifies WebContent processes.

Any modification from client or server triggers a broadcast, prompting all WebContent processes to refresh their caches.

Cookie coordination diagram
Cookie coordination diagram

5. Summary

Cookies are simple to use but involve complex coordination inside WebKit. UIProcess, WebContent, and NetworkProcess determine where cookies reside (memory vs. disk) and how they are synchronized across JavaScript, HTTP headers, and iOS APIs. Developers should handle modifications carefully to avoid unintended side effects.

Supplement: Cross‑Origin Cookie Restrictions

iOS 14 disables cross‑origin requests from carrying cookies (see https://webkit.org/tracking-prevention/).

Reference Links

WebKit source: https://github.com/WebKit/WebKit Apple Swift implementation of NSHTTPCookieStorage:

https://github.com/apple/swift-corelibs-foundation/blob/main/Sources/FoundationNetworking/HTTPCookieStorage.swift

MDN documentation:

https://developer.mozilla.org/zh-CN
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

iOSWebKitWKWebViewCookie ManagementProcess CommunicationHTTP Cookies
Baidu App Technology
Written by

Baidu App Technology

Official Baidu App Tech Account

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.