Skip to content

Commit

Permalink
fix: ios
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Dec 31, 2023
1 parent 69270ba commit 3215c76
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
54 changes: 35 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Then the permission will be asked when the camera is used.
<docgen-index>

* [`open(...)`](#open)
* [`clearCookies()`](#clearcookies)
* [`clearCookies(...)`](#clearcookies)
* [`getCookies(...)`](#getcookies)
* [`close()`](#close)
* [`openWebView(...)`](#openwebview)
Expand Down Expand Up @@ -72,13 +72,17 @@ Open url in a new window fullscreen
--------------------


### clearCookies()
### clearCookies(...)

```typescript
clearCookies() => Promise<any>
clearCookies(options: ClearCookieOptions) => Promise<any>
```

Clear all cookies
Clear cookies of url

| Param | Type |
| ------------- | ----------------------------------------------------------------- |
| **`options`** | <code><a href="#clearcookieoptions">ClearCookieOptions</a></code> |

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

Expand All @@ -90,7 +94,7 @@ Clear all cookies
### getCookies(...)

```typescript
getCookies(options: GetCookieOptions) => Promise<{ cookies: Record<string, string>; }>
getCookies(options: GetCookieOptions) => Promise<Record<string, string>>
```

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

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

--------------------

Expand Down Expand Up @@ -253,12 +257,11 @@ Reload the current web page.
#### Headers


#### GetCookieOptions
#### ClearCookieOptions

| Prop | Type |
| --------------------- | -------------------- |
| **`url`** | <code>string</code> |
| **`includeHttpOnly`** | <code>boolean</code> |
| Prop | Type |
| --------- | ------------------- |
| **`url`** | <code>string</code> |


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


#### GetCookieOptions

| Prop | Type |
| --------------------- | -------------------- |
| **`url`** | <code>string</code> |
| **`includeHttpOnly`** | <code>boolean</code> |


#### OpenWebViewOptions

| Prop | Type | Description | Default | Since |
Expand Down Expand Up @@ -327,14 +338,7 @@ Reload the current web page.
### Type Aliases


#### Record

Construct a type with a set of properties K of type T

<code>{ [P in K]: T; }</code>


#### GetCookieOptions
#### ClearCookieOptions

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

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


#### Record

Construct a type with a set of properties K of type T

<code>{ [P in K]: T; }</code>


#### GetCookieOptions

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


#### UrlChangeListener

<code>(state: <a href="#urlevent">UrlEvent</a>): void</code>
Expand Down
23 changes: 12 additions & 11 deletions ios/Plugin/InAppBrowserPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,37 @@ public class InAppBrowserPlugin: CAPPlugin {
let dataStore = WKWebsiteDataStore.default()
let urlString = call.getString("url") ?? ""

guard let url = URL(string: urlString) else {
guard let url = URL(string: urlString), let hostName = url.host else {
call.reject("Invalid URL")
return
}
let hostName = url.host ?? ""

dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
let domainRecords = records.filter { record in
record.domains.contains(hostName)
dataStore.httpCookieStore.getAllCookies { cookies in
let cookiesToDelete = cookies.filter { cookie in
cookie.domain == hostName || cookie.domain.hasSuffix(".\(hostName)") || hostName.hasSuffix(cookie.domain)
}

dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: domainRecords) {
call.resolve()
for cookie in cookiesToDelete {
dataStore.httpCookieStore.delete(cookie)
}

call.resolve()
}
}

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

guard let url = URL(string: urlString) else {
guard let url = URL(string: urlString), let host = url.host else {
call.reject("Invalid URL")
return
}

WKWebsiteDataStore.default().httpCookieStore.getAllCookies { cookies in
var cookieDict = [String: String]()
for cookie in cookies {
if includeHttpOnly || !cookie.isHTTPOnly {

if (includeHttpOnly || !cookie.isHTTPOnly) && (cookie.domain == host || cookie.domain.hasSuffix(".\(host)") || host.hasSuffix(cookie.domain)) {
cookieDict[cookie.name] = cookie.value
}
}
Expand Down Expand Up @@ -164,7 +165,7 @@ public class InAppBrowserPlugin: CAPPlugin {
self.navigationWebViewController?.navigationBar.isHidden = true
}
if showReloadButton {
var toolbarItems = self.getToolbarItems(toolbarType: toolbarType)
let toolbarItems = self.getToolbarItems(toolbarType: toolbarType)
self.webViewController?.leftNavigaionBarItemTypes = toolbarItems + [.reload]
}
if !self.isPresentAfterPageLoad {
Expand Down

0 comments on commit 3215c76

Please sign in to comment.