Skip to content

Commit 3215c76

Browse files
committed
fix: ios
1 parent 69270ba commit 3215c76

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

README.md

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Then the permission will be asked when the camera is used.
3434
<docgen-index>
3535

3636
* [`open(...)`](#open)
37-
* [`clearCookies()`](#clearcookies)
37+
* [`clearCookies(...)`](#clearcookies)
3838
* [`getCookies(...)`](#getcookies)
3939
* [`close()`](#close)
4040
* [`openWebView(...)`](#openwebview)
@@ -72,13 +72,17 @@ Open url in a new window fullscreen
7272
--------------------
7373

7474

75-
### clearCookies()
75+
### clearCookies(...)
7676

7777
```typescript
78-
clearCookies() => Promise<any>
78+
clearCookies(options: ClearCookieOptions) => Promise<any>
7979
```
8080

81-
Clear all cookies
81+
Clear cookies of url
82+
83+
| Param | Type |
84+
| ------------- | ----------------------------------------------------------------- |
85+
| **`options`** | <code><a href="#clearcookieoptions">ClearCookieOptions</a></code> |
8286

8387
**Returns:** <code>Promise&lt;any&gt;</code>
8488

@@ -90,7 +94,7 @@ Clear all cookies
9094
### getCookies(...)
9195

9296
```typescript
93-
getCookies(options: GetCookieOptions) => Promise<{ cookies: Record<string, string>; }>
97+
getCookies(options: GetCookieOptions) => Promise<Record<string, string>>
9498
```
9599

96100
Get cookies for a specific URL.
@@ -99,7 +103,7 @@ Get cookies for a specific URL.
99103
| ------------- | ------------------------------------------------------------- | -------------------------------------------------- |
100104
| **`options`** | <code><a href="#getcookieoptions">GetCookieOptions</a></code> | The options, including the URL to get cookies for. |
101105

102-
**Returns:** <code>Promise&lt;{ cookies: <a href="#record">Record</a>&lt;string, string&gt;; }&gt;</code>
106+
**Returns:** <code>Promise&lt;<a href="#record">Record</a>&lt;string, string&gt;&gt;</code>
103107

104108
--------------------
105109

@@ -253,12 +257,11 @@ Reload the current web page.
253257
#### Headers
254258

255259

256-
#### GetCookieOptions
260+
#### ClearCookieOptions
257261

258-
| Prop | Type |
259-
| --------------------- | -------------------- |
260-
| **`url`** | <code>string</code> |
261-
| **`includeHttpOnly`** | <code>boolean</code> |
262+
| Prop | Type |
263+
| --------- | ------------------- |
264+
| **`url`** | <code>string</code> |
262265

263266

264267
#### HttpCookie
@@ -270,6 +273,14 @@ Reload the current web page.
270273
| **`value`** | <code>string</code> |
271274

272275

276+
#### GetCookieOptions
277+
278+
| Prop | Type |
279+
| --------------------- | -------------------- |
280+
| **`url`** | <code>string</code> |
281+
| **`includeHttpOnly`** | <code>boolean</code> |
282+
283+
273284
#### OpenWebViewOptions
274285

275286
| Prop | Type | Description | Default | Since |
@@ -327,14 +338,7 @@ Reload the current web page.
327338
### Type Aliases
328339

329340

330-
#### Record
331-
332-
Construct a type with a set of properties K of type T
333-
334-
<code>{ [P in K]: T; }</code>
335-
336-
337-
#### GetCookieOptions
341+
#### ClearCookieOptions
338342

339343
<code><a href="#omit">Omit</a>&lt;<a href="#httpcookie">HttpCookie</a>, 'key' | 'value'&gt;</code>
340344

@@ -360,6 +364,18 @@ From T, pick a set of properties whose keys are in the union K
360364
<code>T extends U ? never : T</code>
361365

362366

367+
#### Record
368+
369+
Construct a type with a set of properties K of type T
370+
371+
<code>{ [P in K]: T; }</code>
372+
373+
374+
#### GetCookieOptions
375+
376+
<code><a href="#omit">Omit</a>&lt;<a href="#httpcookie">HttpCookie</a>, 'key' | 'value'&gt;</code>
377+
378+
363379
#### UrlChangeListener
364380

365381
<code>(state: <a href="#urlevent">UrlEvent</a>): void</code>

ios/Plugin/InAppBrowserPlugin.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,37 @@ public class InAppBrowserPlugin: CAPPlugin {
5353
let dataStore = WKWebsiteDataStore.default()
5454
let urlString = call.getString("url") ?? ""
5555

56-
guard let url = URL(string: urlString) else {
56+
guard let url = URL(string: urlString), let hostName = url.host else {
5757
call.reject("Invalid URL")
5858
return
5959
}
60-
let hostName = url.host ?? ""
61-
62-
dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
63-
let domainRecords = records.filter { record in
64-
record.domains.contains(hostName)
60+
dataStore.httpCookieStore.getAllCookies { cookies in
61+
let cookiesToDelete = cookies.filter { cookie in
62+
cookie.domain == hostName || cookie.domain.hasSuffix(".\(hostName)") || hostName.hasSuffix(cookie.domain)
6563
}
6664

67-
dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: domainRecords) {
68-
call.resolve()
65+
for cookie in cookiesToDelete {
66+
dataStore.httpCookieStore.delete(cookie)
6967
}
68+
69+
call.resolve()
7070
}
7171
}
7272

7373
@objc func getCookies(_ call: CAPPluginCall) {
7474
let urlString = call.getString("url") ?? ""
7575
let includeHttpOnly = call.getBool("includeHttpOnly") ?? true
7676

77-
guard let url = URL(string: urlString) else {
77+
guard let url = URL(string: urlString), let host = url.host else {
7878
call.reject("Invalid URL")
7979
return
8080
}
8181

8282
WKWebsiteDataStore.default().httpCookieStore.getAllCookies { cookies in
8383
var cookieDict = [String: String]()
8484
for cookie in cookies {
85-
if includeHttpOnly || !cookie.isHTTPOnly {
85+
86+
if (includeHttpOnly || !cookie.isHTTPOnly) && (cookie.domain == host || cookie.domain.hasSuffix(".\(host)") || host.hasSuffix(cookie.domain)) {
8687
cookieDict[cookie.name] = cookie.value
8788
}
8889
}
@@ -164,7 +165,7 @@ public class InAppBrowserPlugin: CAPPlugin {
164165
self.navigationWebViewController?.navigationBar.isHidden = true
165166
}
166167
if showReloadButton {
167-
var toolbarItems = self.getToolbarItems(toolbarType: toolbarType)
168+
let toolbarItems = self.getToolbarItems(toolbarType: toolbarType)
168169
self.webViewController?.leftNavigaionBarItemTypes = toolbarItems + [.reload]
169170
}
170171
if !self.isPresentAfterPageLoad {

0 commit comments

Comments
 (0)