Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@capacitor/bowser) Add option to clear all website data while closing iOS browser window #2192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions browser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const openCapacitorSite = async () => {
<docgen-index>

* [`open(...)`](#open)
* [`close()`](#close)
* [`close(...)`](#close)
* [`addListener('browserFinished', ...)`](#addlistenerbrowserfinished-)
* [`addListener('browserPageLoaded', ...)`](#addlistenerbrowserpageloaded-)
* [`removeAllListeners()`](#removealllisteners)
Expand Down Expand Up @@ -62,16 +62,20 @@ Open a page with the specified options.
--------------------


### close()
### close(...)

```typescript
close() => Promise<void>
close(options?: CloseOptions | undefined) => Promise<void>
```

Web & iOS only: Close an open browser window.

No-op on other platforms.

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

**Since:** 1.0.0

--------------------
Expand Down Expand Up @@ -150,6 +154,13 @@ Represents the options passed to `open`.
| **`height`** | <code>number</code> | iOS only: The height the browser when using presentationStyle 'popover' on iPads. Ignored on other platforms. | 4.0.0 |


#### CloseOptions

| Prop | Type | Description |
| ---------------------- | -------------------- | ------------------------------------------------------------------- |
| **`clearWebsiteData`** | <code>boolean</code> | iOS only: clear all website data before close default: null / false |


#### PluginListenerHandle

| Prop | Type |
Expand Down
7 changes: 6 additions & 1 deletion browser/ios/Sources/BrowserPlugin/Browser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ import SafariServices
return false
}

@objc public func cleanup() {
@objc public func cleanup(clearWebsiteData: Bool) {
if clearWebsiteData {
if #available(iOS 16.0, *) {
SFSafariViewController.DataStore.default.clearWebsiteData(completionHandler: {})
}
}
safariViewController = nil
}

Expand Down
4 changes: 3 additions & 1 deletion browser/ios/Sources/BrowserPlugin/BrowserPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public class CAPBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
@objc func close(_ call: CAPPluginCall) {
DispatchQueue.main.async { [weak self] in
if self?.implementation.viewController != nil {

self?.bridge?.dismissVC(animated: true) {
call.resolve()
self?.implementation.cleanup()
let clearWebsiteData = call.getBool("clearWebsiteData") ?? false
self?.implementation.cleanup(clearWebsiteData: clearWebsiteData)
}
} else {
call.reject("No active window to close!")
Expand Down
11 changes: 10 additions & 1 deletion browser/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface BrowserPlugin {
*
* @since 1.0.0
*/
close(): Promise<void>;
close(options?: CloseOptions): Promise<void>;

/**
* Android & iOS only: Listen for the browser finished event.
Expand Down Expand Up @@ -107,6 +107,15 @@ export interface OpenOptions {
height?: number;
}

export interface CloseOptions {
/**
* iOS only: clear all website data before close
*
* default: null / false
*/
clearWebsiteData?: boolean;
}

/**
* @deprecated Use `OpenOptions`.
* @since 1.0.0
Expand Down