Skip to content

Commit f2ba3ac

Browse files
feat(ios-common-cookies): add clearAllCookies function
Hi there! πŸ‘‹ This pull request adds a new feature to the iOS Common Cookies module, allowing all cookies to be cleared from both `HTTPCookieStorage` and the WKWebView cookie store. The most important changes include the implementation of the new `clearAllCookies` method in both the native Swift module and the TypeScript interface, as well as the introduction of a new result type for this operation. **New Feature: Cookie Clearing** * Added a new async function `clearAllCookies` to the `iOSCommonCookiesModule` Swift class, which clears all cookies from `HTTPCookieStorage` and the WKWebView cookie store, and returns the number of cookies cleared from each. * Added the `clearAllCookies()` method to the TypeScript interface in `iOSCommonCookies.ts`, returning a `Promise<CookieClearResult>`. * Introduced a new `CookieClearResult` type in `iOSCommonCookies.types.ts` to represent the result of the cookie clearing operation, including counts for both stores. **TypeScript Typings Update** * Updated the import statement in `iOSCommonCookies.ts` to include the new `CookieClearResult` type.
1 parent 6101a58 commit f2ba3ac

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

β€Žpackages/public/ios-common-cookies/ios/iOSCommonCookiesModule.swiftβ€Ž

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ public class iOSCommonCookiesModule: Module {
4545
}
4646
}
4747

48+
// Clear all cookies from HTTPCookieStorage and WKWebView cookie store.
49+
AsyncFunction("clearAllCookies") { (promise: Promise) in
50+
DispatchQueue.main.async {
51+
// 1) Clear HTTPCookieStorage
52+
let httpStorage = HTTPCookieStorage.shared
53+
let httpCookies = httpStorage.cookies ?? []
54+
let httpCount = httpCookies.count
55+
for cookie in httpCookies {
56+
httpStorage.deleteCookie(cookie)
57+
}
58+
59+
// 2) Clear WKWebView cookie store
60+
let cookieStore = WKWebsiteDataStore.default().httpCookieStore
61+
cookieStore.getAllCookies { wkCookies in
62+
let wkCount = wkCookies.count
63+
let group = DispatchGroup()
64+
65+
for cookie in wkCookies {
66+
group.enter()
67+
cookieStore.delete(cookie) {
68+
group.leave()
69+
}
70+
}
71+
72+
group.notify(queue: .main) {
73+
promise.resolve([
74+
"clearedHTTP": httpCount,
75+
"clearedWebView": wkCount
76+
])
77+
}
78+
}
79+
}
80+
}
81+
4882
// Best-effort two-way sync: HTTP -> WebView, then WebView -> HTTP.
4983
AsyncFunction("syncCookiesBidirectional") { (promise: Promise) in
5084
DispatchQueue.main.async {

β€Žpackages/public/ios-common-cookies/src/iOSCommonCookies.tsβ€Ž

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NativeModule, requireNativeModule } from "expo-modules-core";
22

3-
import type { CookieBidirectionalSyncResult, CookieSyncResult } from "./iOSCommonCookies.types";
3+
import type { CookieBidirectionalSyncResult, CookieSyncResult, CookieClearResult } from "./iOSCommonCookies.types";
44

55
declare class iOSCommonCookiesModule extends NativeModule {
66
/** Copy cookies from HTTPCookieStorage -> WKWebsiteDataStore.default().httpCookieStore */
@@ -9,6 +9,9 @@ declare class iOSCommonCookiesModule extends NativeModule {
99
/** Copy cookies from WKWebsiteDataStore.default().httpCookieStore -> HTTPCookieStorage */
1010
syncCookiesFromWebView(): Promise<CookieSyncResult>;
1111

12+
/** Clear all cookies from HTTPCookieStorage and WKWebView cookie store */
13+
clearAllCookies(): Promise<CookieClearResult>;
14+
1215
/** Best-effort two-way sync (HTTP -> WebView, then WebView -> HTTP) */
1316
syncCookiesBidirectional(): Promise<CookieBidirectionalSyncResult>;
1417
}

β€Žpackages/public/ios-common-cookies/src/iOSCommonCookies.types.tsβ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ export type CookieBidirectionalSyncResult = CookieSyncResult & {
77
fromHTTP: number;
88
fromWebView: number;
99
};
10+
11+
export type CookieClearResult = {
12+
clearedHTTP: number;
13+
clearedWebView: number;
14+
};

0 commit comments

Comments
Β (0)