@capawesome/capacitor-in-app-browser 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -48
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/InAppBrowser.java +25 -5
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/InAppBrowserPlugin.java +37 -10
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/WebViewDialog.java +40 -2
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/events/{MessageReceivedEvent.java → BrowserMessageReceivedEvent.java} +2 -2
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/events/{BrowserPageNavigationCompletedEvent.java → BrowserNavigationCompletedEvent.java} +2 -2
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/events/BrowserUrlChangedEvent.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/options/OpenInWebViewOptions.java +7 -0
- package/dist/docs.json +114 -24
- package/dist/esm/definitions.d.ts +62 -12
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/web.d.ts +1 -0
- package/dist/esm/web.js +3 -0
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +3 -0
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +3 -0
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/Classes/Events/{MessageReceivedEvent.swift → BrowserMessageReceivedEvent.swift} +1 -1
- package/ios/Plugin/Classes/Events/{BrowserPageNavigationCompletedEvent.swift → BrowserNavigationCompletedEvent.swift} +1 -1
- package/ios/Plugin/Classes/Events/BrowserUrlChangedEvent.swift +16 -0
- package/ios/Plugin/Classes/Options/OpenInWebViewOptions.swift +2 -0
- package/ios/Plugin/Classes/WebViewController.swift +18 -2
- package/ios/Plugin/InAppBrowser.swift +45 -4
- package/ios/Plugin/InAppBrowserPlugin.swift +25 -9
- package/package.json +2 -2
|
@@ -5,8 +5,9 @@ import WebKit
|
|
|
5
5
|
@objc public class WebViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler, WKUIDelegate {
|
|
6
6
|
var onClosed: ((WebViewController) -> Void)?
|
|
7
7
|
var onMessageReceived: ((String) -> Void)?
|
|
8
|
+
var onNavigationCompleted: ((String) -> Void)?
|
|
8
9
|
var onPageLoaded: (() -> Void)?
|
|
9
|
-
var
|
|
10
|
+
var onUrlChanged: ((String) -> Void)?
|
|
10
11
|
|
|
11
12
|
private static let bridgeJavaScript =
|
|
12
13
|
"window.CapacitorInAppBrowser = window.CapacitorInAppBrowser || { postMessage: function (data) "
|
|
@@ -20,6 +21,7 @@ import WebKit
|
|
|
20
21
|
private var canGoForwardObservation: NSKeyValueObservation?
|
|
21
22
|
private var forwardButtonItem: UIBarButtonItem?
|
|
22
23
|
private var initialLoadNotified = false
|
|
24
|
+
private var lastNotifiedUrl: String?
|
|
23
25
|
private var titleObservation: NSKeyValueObservation?
|
|
24
26
|
private var urlObservation: NSKeyValueObservation?
|
|
25
27
|
private var webView: WKWebView?
|
|
@@ -43,6 +45,11 @@ import WebKit
|
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
public func handleCloseWithoutPresentation() {
|
|
49
|
+
cleanup()
|
|
50
|
+
onClosed?(self)
|
|
51
|
+
}
|
|
52
|
+
|
|
46
53
|
public func postMessage(_ data: String) {
|
|
47
54
|
webView?.evaluateJavaScript(
|
|
48
55
|
"window.dispatchEvent(new CustomEvent('capacitorInAppBrowserMessage', { detail: \(data) }));",
|
|
@@ -95,7 +102,7 @@ import WebKit
|
|
|
95
102
|
onPageLoaded?()
|
|
96
103
|
}
|
|
97
104
|
if let url = webView.url?.absoluteString {
|
|
98
|
-
|
|
105
|
+
onNavigationCompleted?(url)
|
|
99
106
|
}
|
|
100
107
|
}
|
|
101
108
|
|
|
@@ -178,6 +185,7 @@ import WebKit
|
|
|
178
185
|
self?.updateTitle()
|
|
179
186
|
}
|
|
180
187
|
urlObservation = webView.observe(\.url, options: [.new]) { [weak self] _, _ in
|
|
188
|
+
self?.handleUrlChanged()
|
|
181
189
|
self?.updateTitle()
|
|
182
190
|
}
|
|
183
191
|
return webView
|
|
@@ -199,6 +207,14 @@ import WebKit
|
|
|
199
207
|
}
|
|
200
208
|
}
|
|
201
209
|
|
|
210
|
+
private func handleUrlChanged() {
|
|
211
|
+
guard let url = webView?.url?.absoluteString, url != lastNotifiedUrl else {
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
lastNotifiedUrl = url
|
|
215
|
+
onUrlChanged?(url)
|
|
216
|
+
}
|
|
217
|
+
|
|
202
218
|
private func loadUrl() {
|
|
203
219
|
var request = URLRequest(url: options.url)
|
|
204
220
|
for (key, value) in options.headers {
|
|
@@ -9,6 +9,7 @@ import WebKit
|
|
|
9
9
|
|
|
10
10
|
private var safariViewController: SFSafariViewController?
|
|
11
11
|
private var webViewController: WebViewController?
|
|
12
|
+
private var webViewNavigationController: UINavigationController?
|
|
12
13
|
|
|
13
14
|
init(plugin: InAppBrowserPlugin) {
|
|
14
15
|
self.plugin = plugin
|
|
@@ -35,6 +36,11 @@ import WebKit
|
|
|
35
36
|
@objc public func close(completion: @escaping (_ error: Error?) -> Void) {
|
|
36
37
|
DispatchQueue.main.async {
|
|
37
38
|
if let webViewController = self.webViewController {
|
|
39
|
+
if self.webViewNavigationController?.presentingViewController == nil {
|
|
40
|
+
webViewController.handleCloseWithoutPresentation()
|
|
41
|
+
completion(nil)
|
|
42
|
+
return
|
|
43
|
+
}
|
|
38
44
|
webViewController.dismiss(animated: true) {
|
|
39
45
|
completion(nil)
|
|
40
46
|
}
|
|
@@ -122,23 +128,33 @@ import WebKit
|
|
|
122
128
|
}
|
|
123
129
|
if self.webViewController === viewController {
|
|
124
130
|
self.webViewController = nil
|
|
131
|
+
self.webViewNavigationController = nil
|
|
125
132
|
}
|
|
126
133
|
self.plugin.notifyBrowserClosedListeners()
|
|
127
134
|
}
|
|
128
135
|
webViewController.onMessageReceived = { [weak self] data in
|
|
129
136
|
self?.handleMessageReceived(data)
|
|
130
137
|
}
|
|
138
|
+
webViewController.onNavigationCompleted = { [weak self] url in
|
|
139
|
+
self?.plugin.notifyBrowserNavigationCompletedListeners(BrowserNavigationCompletedEvent(url: url))
|
|
140
|
+
}
|
|
131
141
|
webViewController.onPageLoaded = { [weak self] in
|
|
132
142
|
self?.plugin.notifyBrowserPageLoadedListeners()
|
|
133
143
|
}
|
|
134
|
-
webViewController.
|
|
135
|
-
self?.plugin.
|
|
144
|
+
webViewController.onUrlChanged = { [weak self] url in
|
|
145
|
+
self?.plugin.notifyBrowserUrlChangedListeners(BrowserUrlChangedEvent(url: url))
|
|
136
146
|
}
|
|
137
147
|
let navigationController = UINavigationController(rootViewController: webViewController)
|
|
138
148
|
navigationController.modalPresentationStyle = .fullScreen
|
|
139
149
|
navigationController.setNavigationBarHidden(!options.toolbar.visible, animated: false)
|
|
140
150
|
self.webViewController = webViewController
|
|
141
|
-
|
|
151
|
+
self.webViewNavigationController = navigationController
|
|
152
|
+
if options.visible {
|
|
153
|
+
bridgeViewController.present(navigationController, animated: true) {
|
|
154
|
+
completion(nil)
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
webViewController.loadViewIfNeeded()
|
|
142
158
|
completion(nil)
|
|
143
159
|
}
|
|
144
160
|
}
|
|
@@ -168,8 +184,33 @@ import WebKit
|
|
|
168
184
|
plugin.notifyBrowserClosedListeners()
|
|
169
185
|
}
|
|
170
186
|
|
|
187
|
+
@objc public func show(completion: @escaping (_ error: Error?) -> Void) {
|
|
188
|
+
DispatchQueue.main.async {
|
|
189
|
+
guard let navigationController = self.webViewNavigationController else {
|
|
190
|
+
completion(CustomError.noBrowserOpen)
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
if navigationController.presentingViewController != nil {
|
|
194
|
+
completion(nil)
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
guard let bridgeViewController = self.plugin.bridge?.viewController else {
|
|
198
|
+
completion(CustomError.noBrowserOpen)
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
bridgeViewController.present(navigationController, animated: true) {
|
|
202
|
+
completion(nil)
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
171
207
|
private func dismissActiveBrowser(_ completion: @escaping () -> Void) {
|
|
172
208
|
if let webViewController = self.webViewController {
|
|
209
|
+
if self.webViewNavigationController?.presentingViewController == nil {
|
|
210
|
+
webViewController.handleCloseWithoutPresentation()
|
|
211
|
+
completion()
|
|
212
|
+
return
|
|
213
|
+
}
|
|
173
214
|
webViewController.dismiss(animated: false) {
|
|
174
215
|
completion()
|
|
175
216
|
}
|
|
@@ -189,6 +230,6 @@ import WebKit
|
|
|
189
230
|
if let jsonData = data.data(using: .utf8), let parsedValue = try? JSONSerialization.jsonObject(with: jsonData, options: [.fragmentsAllowed]) {
|
|
190
231
|
value = parsedValue
|
|
191
232
|
}
|
|
192
|
-
plugin.
|
|
233
|
+
plugin.notifyBrowserMessageReceivedListeners(BrowserMessageReceivedEvent(data: value))
|
|
193
234
|
}
|
|
194
235
|
}
|
|
@@ -14,13 +14,15 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
14
14
|
CAPPluginMethod(name: "openInExternalBrowser", returnType: CAPPluginReturnPromise),
|
|
15
15
|
CAPPluginMethod(name: "openInSystemBrowser", returnType: CAPPluginReturnPromise),
|
|
16
16
|
CAPPluginMethod(name: "openInWebView", returnType: CAPPluginReturnPromise),
|
|
17
|
-
CAPPluginMethod(name: "postMessage", returnType: CAPPluginReturnPromise)
|
|
17
|
+
CAPPluginMethod(name: "postMessage", returnType: CAPPluginReturnPromise),
|
|
18
|
+
CAPPluginMethod(name: "show", returnType: CAPPluginReturnPromise)
|
|
18
19
|
]
|
|
19
20
|
|
|
20
21
|
public static let eventBrowserClosed = "browserClosed"
|
|
22
|
+
public static let eventBrowserMessageReceived = "browserMessageReceived"
|
|
23
|
+
public static let eventBrowserNavigationCompleted = "browserNavigationCompleted"
|
|
21
24
|
public static let eventBrowserPageLoaded = "browserPageLoaded"
|
|
22
|
-
public static let
|
|
23
|
-
public static let eventMessageReceived = "messageReceived"
|
|
25
|
+
public static let eventBrowserUrlChanged = "browserUrlChanged"
|
|
24
26
|
public static let tag = "InAppBrowserPlugin"
|
|
25
27
|
|
|
26
28
|
private var implementation: InAppBrowser?
|
|
@@ -95,16 +97,20 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
95
97
|
self.notifyListeners(Self.eventBrowserClosed, data: [:])
|
|
96
98
|
}
|
|
97
99
|
|
|
98
|
-
@objc public func
|
|
99
|
-
self.notifyListeners(Self.
|
|
100
|
+
@objc public func notifyBrowserMessageReceivedListeners(_ event: BrowserMessageReceivedEvent) {
|
|
101
|
+
self.notifyListeners(Self.eventBrowserMessageReceived, data: event.toJSObject() as? [String: Any])
|
|
100
102
|
}
|
|
101
103
|
|
|
102
|
-
@objc public func
|
|
103
|
-
self.notifyListeners(Self.
|
|
104
|
+
@objc public func notifyBrowserNavigationCompletedListeners(_ event: BrowserNavigationCompletedEvent) {
|
|
105
|
+
self.notifyListeners(Self.eventBrowserNavigationCompleted, data: event.toJSObject() as? [String: Any])
|
|
104
106
|
}
|
|
105
107
|
|
|
106
|
-
@objc public func
|
|
107
|
-
self.notifyListeners(Self.
|
|
108
|
+
@objc public func notifyBrowserPageLoadedListeners() {
|
|
109
|
+
self.notifyListeners(Self.eventBrowserPageLoaded, data: [:])
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@objc public func notifyBrowserUrlChangedListeners(_ event: BrowserUrlChangedEvent) {
|
|
113
|
+
self.notifyListeners(Self.eventBrowserUrlChanged, data: event.toJSObject() as? [String: Any])
|
|
108
114
|
}
|
|
109
115
|
|
|
110
116
|
@objc func openInExternalBrowser(_ call: CAPPluginCall) {
|
|
@@ -171,6 +177,16 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
171
177
|
}
|
|
172
178
|
}
|
|
173
179
|
|
|
180
|
+
@objc func show(_ call: CAPPluginCall) {
|
|
181
|
+
implementation?.show(completion: { error in
|
|
182
|
+
if let error = error {
|
|
183
|
+
self.rejectCall(call, error)
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
self.resolveCall(call)
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
174
190
|
private func rejectCall(_ call: CAPPluginCall, _ error: Error) {
|
|
175
191
|
CAPLog.print("[", InAppBrowserPlugin.tag, "] ", error)
|
|
176
192
|
let code = (error as? CustomError)?.code
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capawesome/capacitor-in-app-browser",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Capacitor plugin to open URLs in the external browser, the system browser or an embedded web view.",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"url": "https://opencollective.com/capawesome"
|
|
34
34
|
}
|
|
35
35
|
],
|
|
36
|
-
"homepage": "https://capawesome.io/docs/
|
|
36
|
+
"homepage": "https://capawesome.io/docs/sdks/capacitor/in-app-browser/",
|
|
37
37
|
"keywords": [
|
|
38
38
|
"capacitor",
|
|
39
39
|
"plugin",
|