From c5eae97a6aa207c9e36121e61530c395a28fffa8 Mon Sep 17 00:00:00 2001 From: malua Date: Mon, 30 Dec 2024 13:34:06 +0100 Subject: [PATCH 1/2] feat: close webview with window.AndroidInterface.close() from within --- .../capacitor_inappbrowser/WebViewDialog.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java b/android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java index 3b565bb2..2e5e32b2 100644 --- a/android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java +++ b/android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java @@ -132,6 +132,21 @@ public void postMessage(String message) { // Handle message from JavaScript _options.getCallbacks().javascriptCallback(message); } + + @JavascriptInterface + public void close() { + // close webview + activity.runOnUiThread( + new Runnable() { + @Override + public void run() { + dismiss(); + _options.getCallbacks().closeEvent(_webView.getUrl()); + _webView.destroy(); + } + } + ); + } } public class PreShowScriptInterface { From 50349975e5a0b81595f6f91ea0747910cf9c7aeb Mon Sep 17 00:00:00 2001 From: malua Date: Mon, 30 Dec 2024 14:16:17 +0100 Subject: [PATCH 2/2] feat: add ability to close webview from javascript on ios --- ios/Plugin/WKWebViewController.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ios/Plugin/WKWebViewController.swift b/ios/Plugin/WKWebViewController.swift index 3141567d..83c28675 100644 --- a/ios/Plugin/WKWebViewController.swift +++ b/ios/Plugin/WKWebViewController.swift @@ -267,6 +267,8 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler { } print("[InAppBrowser - preShowScriptError]: Error!!!!") semaphore.signal() + } else if message.name == "close" { + closeView() } } @@ -299,6 +301,7 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler { userContentController.add(self, name: "messageHandler") userContentController.add(self, name: "preShowScriptError") userContentController.add(self, name: "preShowScriptSuccess") + userContentController.add(self, name: "close") webConfiguration.userContentController = userContentController let webView = WKWebView(frame: .zero, configuration: webConfiguration)