@capgo/inappbrowser 8.1.12 → 8.1.14
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 +86 -54
- package/android/src/main/java/ee/forgr/capacitor_inappbrowser/InAppBrowserPlugin.java +184 -40
- package/android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java +20 -4
- package/dist/docs.json +89 -33
- package/dist/esm/definitions.d.ts +57 -10
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift +257 -48
- package/ios/Sources/InAppBrowserPlugin/WKWebViewController.swift +33 -15
- package/package.json +1 -1
|
@@ -28,7 +28,7 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
28
28
|
case aware = "AWARE"
|
|
29
29
|
case fakeVisible = "FAKE_VISIBLE"
|
|
30
30
|
}
|
|
31
|
-
private let pluginVersion: String = "8.1.
|
|
31
|
+
private let pluginVersion: String = "8.1.14"
|
|
32
32
|
public let identifier = "InAppBrowserPlugin"
|
|
33
33
|
public let jsName = "InAppBrowser"
|
|
34
34
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
@@ -50,6 +50,7 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
50
50
|
CAPPluginMethod(name: "getPluginVersion", returnType: CAPPluginReturnPromise)
|
|
51
51
|
]
|
|
52
52
|
var navigationWebViewController: UINavigationController?
|
|
53
|
+
private var navigationControllers: [String: UINavigationController] = [:]
|
|
53
54
|
private var privacyScreen: UIImageView?
|
|
54
55
|
private var isSetupDone = false
|
|
55
56
|
var currentPluginCall: CAPPluginCall?
|
|
@@ -57,6 +58,9 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
57
58
|
var isHidden = false
|
|
58
59
|
var invisibilityMode: InvisibilityMode = .aware
|
|
59
60
|
var webViewController: WKWebViewController?
|
|
61
|
+
private var webViewControllers: [String: WKWebViewController] = [:]
|
|
62
|
+
private var webViewStack: [String] = []
|
|
63
|
+
private var activeWebViewId: String?
|
|
60
64
|
private weak var presentationContainerView: UIView?
|
|
61
65
|
private var presentationContainerWasInteractive = true
|
|
62
66
|
private var presentationContainerPreviousAlpha: CGFloat = 1
|
|
@@ -77,13 +81,74 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
77
81
|
#endif
|
|
78
82
|
}
|
|
79
83
|
|
|
80
|
-
func
|
|
81
|
-
|
|
84
|
+
private func registerWebView(id: String, webView: WKWebViewController, navigationController: UINavigationController) {
|
|
85
|
+
webViewControllers[id] = webView
|
|
86
|
+
navigationControllers[id] = navigationController
|
|
87
|
+
webViewStack.removeAll { $0 == id }
|
|
88
|
+
webViewStack.append(id)
|
|
89
|
+
activeWebViewId = id
|
|
90
|
+
self.webViewController = webView
|
|
91
|
+
self.navigationWebViewController = navigationController
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private func unregisterWebView(id: String) {
|
|
95
|
+
webViewControllers[id] = nil
|
|
96
|
+
navigationControllers[id] = nil
|
|
97
|
+
webViewStack.removeAll { $0 == id }
|
|
98
|
+
activeWebViewId = webViewStack.last
|
|
99
|
+
if let activeId = activeWebViewId {
|
|
100
|
+
self.webViewController = webViewControllers[activeId]
|
|
101
|
+
self.navigationWebViewController = navigationControllers[activeId]
|
|
102
|
+
} else {
|
|
103
|
+
self.webViewController = nil
|
|
104
|
+
self.navigationWebViewController = nil
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private func resolveWebViewController(for id: String?) -> WKWebViewController? {
|
|
109
|
+
if let id {
|
|
110
|
+
return webViewControllers[id]
|
|
111
|
+
}
|
|
112
|
+
return webViewController
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private func dataStores(for targetId: String?) -> [WKWebsiteDataStore] {
|
|
116
|
+
if let targetId {
|
|
117
|
+
guard let controller = webViewControllers[targetId],
|
|
118
|
+
let store = controller.websiteDataStore() else {
|
|
119
|
+
return []
|
|
120
|
+
}
|
|
121
|
+
return [store]
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let controllers = Array(webViewControllers.values)
|
|
125
|
+
if controllers.isEmpty {
|
|
126
|
+
return [WKWebsiteDataStore.default()]
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
var seen = Set<ObjectIdentifier>()
|
|
130
|
+
var stores: [WKWebsiteDataStore] = []
|
|
131
|
+
for controller in controllers {
|
|
132
|
+
if let store = controller.websiteDataStore() {
|
|
133
|
+
let identifier = ObjectIdentifier(store)
|
|
134
|
+
if seen.insert(identifier).inserted {
|
|
135
|
+
stores.append(store)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return stores
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
func presentView(webViewId: String? = nil, isAnimated: Bool = true) {
|
|
143
|
+
let resolvedId = webViewId ?? activeWebViewId
|
|
144
|
+
let navigationController = resolvedId.flatMap { navigationControllers[$0] } ?? self.navigationWebViewController
|
|
145
|
+
guard let navigationController else {
|
|
82
146
|
self.currentPluginCall?.reject("Navigation controller is not initialized")
|
|
83
147
|
return
|
|
84
148
|
}
|
|
85
149
|
|
|
86
|
-
self.bridge?.viewController?.
|
|
150
|
+
let presenter = self.bridge?.viewController?.presentedViewController ?? self.bridge?.viewController
|
|
151
|
+
presenter?.present(navigationController, animated: isAnimated, completion: {
|
|
87
152
|
self.currentPluginCall?.resolve()
|
|
88
153
|
})
|
|
89
154
|
}
|
|
@@ -146,13 +211,42 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
146
211
|
webView.isUserInteractionEnabled = true
|
|
147
212
|
}
|
|
148
213
|
|
|
214
|
+
func handleWebViewDidClose(id: String, url: String) {
|
|
215
|
+
if !id.isEmpty, webViewControllers[id] != nil {
|
|
216
|
+
self.notifyListeners("closeEvent", data: ["id": id, "url": url])
|
|
217
|
+
unregisterWebView(id: id)
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
self.notifyListeners("closeEvent", data: ["url": url])
|
|
222
|
+
self.webViewController = nil
|
|
223
|
+
self.navigationWebViewController = nil
|
|
224
|
+
}
|
|
225
|
+
|
|
149
226
|
@objc func clearAllCookies(_ call: CAPPluginCall) {
|
|
150
227
|
DispatchQueue.main.async {
|
|
151
|
-
let
|
|
152
|
-
let
|
|
228
|
+
let targetId = call.getString("id")
|
|
229
|
+
if let targetId, self.webViewControllers[targetId] == nil {
|
|
230
|
+
call.reject("WebView is not initialized")
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
let dataStores = self.dataStores(for: targetId)
|
|
235
|
+
if dataStores.isEmpty {
|
|
236
|
+
call.reject("WebView is not initialized")
|
|
237
|
+
return
|
|
238
|
+
}
|
|
153
239
|
|
|
154
|
-
|
|
155
|
-
|
|
240
|
+
let dataTypes = Set([WKWebsiteDataTypeCookies])
|
|
241
|
+
let group = DispatchGroup()
|
|
242
|
+
for dataStore in dataStores {
|
|
243
|
+
group.enter()
|
|
244
|
+
dataStore.removeData(ofTypes: dataTypes,
|
|
245
|
+
modifiedSince: Date(timeIntervalSince1970: 0)) {
|
|
246
|
+
group.leave()
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
group.notify(queue: .main) {
|
|
156
250
|
call.resolve()
|
|
157
251
|
}
|
|
158
252
|
}
|
|
@@ -160,11 +254,28 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
160
254
|
|
|
161
255
|
@objc func clearCache(_ call: CAPPluginCall) {
|
|
162
256
|
DispatchQueue.main.async {
|
|
163
|
-
let
|
|
164
|
-
let
|
|
257
|
+
let targetId = call.getString("id")
|
|
258
|
+
if let targetId, self.webViewControllers[targetId] == nil {
|
|
259
|
+
call.reject("WebView is not initialized")
|
|
260
|
+
return
|
|
261
|
+
}
|
|
165
262
|
|
|
166
|
-
|
|
167
|
-
|
|
263
|
+
let dataStores = self.dataStores(for: targetId)
|
|
264
|
+
if dataStores.isEmpty {
|
|
265
|
+
call.reject("WebView is not initialized")
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let dataTypes = Set([WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
|
|
270
|
+
let group = DispatchGroup()
|
|
271
|
+
for dataStore in dataStores {
|
|
272
|
+
group.enter()
|
|
273
|
+
dataStore.removeData(ofTypes: dataTypes,
|
|
274
|
+
modifiedSince: Date(timeIntervalSince1970: 0)) {
|
|
275
|
+
group.leave()
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
group.notify(queue: .main) {
|
|
168
279
|
call.resolve()
|
|
169
280
|
}
|
|
170
281
|
}
|
|
@@ -178,20 +289,39 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
178
289
|
}
|
|
179
290
|
|
|
180
291
|
DispatchQueue.main.async {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
292
|
+
let targetId = call.getString("id")
|
|
293
|
+
if let targetId, self.webViewControllers[targetId] == nil {
|
|
294
|
+
call.reject("WebView is not initialized")
|
|
295
|
+
return
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
let dataStores = self.dataStores(for: targetId)
|
|
299
|
+
if dataStores.isEmpty {
|
|
300
|
+
call.reject("WebView is not initialized")
|
|
301
|
+
return
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
let outerGroup = DispatchGroup()
|
|
305
|
+
for dataStore in dataStores {
|
|
306
|
+
outerGroup.enter()
|
|
307
|
+
dataStore.httpCookieStore.getAllCookies { cookies in
|
|
308
|
+
let innerGroup = DispatchGroup()
|
|
309
|
+
for cookie in cookies {
|
|
310
|
+
if cookie.domain == host || cookie.domain.hasSuffix(".\(host)") || host.hasSuffix(cookie.domain) {
|
|
311
|
+
innerGroup.enter()
|
|
312
|
+
dataStore.httpCookieStore.delete(cookie) {
|
|
313
|
+
innerGroup.leave()
|
|
314
|
+
}
|
|
188
315
|
}
|
|
189
316
|
}
|
|
317
|
+
innerGroup.notify(queue: .main) {
|
|
318
|
+
outerGroup.leave()
|
|
319
|
+
}
|
|
190
320
|
}
|
|
321
|
+
}
|
|
191
322
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
}
|
|
323
|
+
outerGroup.notify(queue: .main) {
|
|
324
|
+
call.resolve()
|
|
195
325
|
}
|
|
196
326
|
}
|
|
197
327
|
}
|
|
@@ -236,6 +366,8 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
236
366
|
return
|
|
237
367
|
}
|
|
238
368
|
|
|
369
|
+
let webViewId = UUID().uuidString
|
|
370
|
+
|
|
239
371
|
var buttonNearDoneIcon: UIImage?
|
|
240
372
|
if let buttonNearDoneSettings = call.getObject("buttonNearDone") {
|
|
241
373
|
guard let iosSettingsRaw = buttonNearDoneSettings["ios"] else {
|
|
@@ -478,6 +610,8 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
478
610
|
return
|
|
479
611
|
}
|
|
480
612
|
|
|
613
|
+
webViewController.allowWebViewJsVisibilityControl = allowWebViewJsVisibilityControl
|
|
614
|
+
webViewController.instanceId = webViewId
|
|
481
615
|
webViewController.allowWebViewJsVisibilityControl = allowWebViewJsVisibilityControl
|
|
482
616
|
|
|
483
617
|
// Set dimensions if provided
|
|
@@ -630,6 +764,9 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
630
764
|
}
|
|
631
765
|
|
|
632
766
|
self.navigationWebViewController = UINavigationController.init(rootViewController: webViewController)
|
|
767
|
+
if let navigationController = self.navigationWebViewController {
|
|
768
|
+
self.registerWebView(id: webViewId, webView: webViewController, navigationController: navigationController)
|
|
769
|
+
}
|
|
633
770
|
self.navigationWebViewController?.navigationBar.isTranslucent = false
|
|
634
771
|
self.navigationWebViewController?.toolbar.isTranslucent = false
|
|
635
772
|
|
|
@@ -780,15 +917,16 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
780
917
|
return
|
|
781
918
|
}
|
|
782
919
|
} else if !self.isPresentAfterPageLoad {
|
|
783
|
-
self.presentView(isAnimated: isAnimated)
|
|
920
|
+
self.presentView(webViewId: webViewId, isAnimated: isAnimated)
|
|
784
921
|
}
|
|
785
|
-
call.resolve()
|
|
922
|
+
call.resolve(["id": webViewId])
|
|
786
923
|
}
|
|
787
924
|
}
|
|
788
925
|
|
|
789
926
|
@objc func goBack(_ call: CAPPluginCall) {
|
|
790
927
|
DispatchQueue.main.async {
|
|
791
|
-
|
|
928
|
+
let targetId = call.getString("id") ?? self.activeWebViewId
|
|
929
|
+
guard let webViewController = self.resolveWebViewController(for: targetId) else {
|
|
792
930
|
call.resolve(["canGoBack": false])
|
|
793
931
|
return
|
|
794
932
|
}
|
|
@@ -799,8 +937,16 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
799
937
|
}
|
|
800
938
|
|
|
801
939
|
@objc func reload(_ call: CAPPluginCall) {
|
|
802
|
-
|
|
803
|
-
|
|
940
|
+
DispatchQueue.main.async {
|
|
941
|
+
let targetId = call.getString("id") ?? self.activeWebViewId
|
|
942
|
+
guard let webViewController = self.resolveWebViewController(for: targetId) else {
|
|
943
|
+
call.reject("WebView is not initialized")
|
|
944
|
+
return
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
webViewController.reload()
|
|
948
|
+
call.resolve()
|
|
949
|
+
}
|
|
804
950
|
}
|
|
805
951
|
|
|
806
952
|
@objc func setUrl(_ call: CAPPluginCall) {
|
|
@@ -814,7 +960,13 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
814
960
|
return
|
|
815
961
|
}
|
|
816
962
|
|
|
817
|
-
|
|
963
|
+
let targetId = call.getString("id")
|
|
964
|
+
guard let webViewController = self.resolveWebViewController(for: targetId) else {
|
|
965
|
+
call.reject("WebView is not initialized")
|
|
966
|
+
return
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
webViewController.load(remote: url)
|
|
818
970
|
call.resolve()
|
|
819
971
|
}
|
|
820
972
|
|
|
@@ -894,7 +1046,28 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
894
1046
|
return
|
|
895
1047
|
}
|
|
896
1048
|
DispatchQueue.main.async {
|
|
897
|
-
|
|
1049
|
+
if let targetId = call.getString("id") {
|
|
1050
|
+
guard let webViewController = self.webViewControllers[targetId] else {
|
|
1051
|
+
call.reject("WebView is not initialized")
|
|
1052
|
+
return
|
|
1053
|
+
}
|
|
1054
|
+
webViewController.executeScript(script: script)
|
|
1055
|
+
call.resolve()
|
|
1056
|
+
return
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
if !self.webViewControllers.isEmpty {
|
|
1060
|
+
self.webViewControllers.values.forEach { $0.executeScript(script: script) }
|
|
1061
|
+
call.resolve()
|
|
1062
|
+
return
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
guard let webViewController = self.webViewController else {
|
|
1066
|
+
call.reject("WebView is not initialized")
|
|
1067
|
+
return
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
webViewController.executeScript(script: script)
|
|
898
1071
|
call.resolve()
|
|
899
1072
|
}
|
|
900
1073
|
}
|
|
@@ -909,9 +1082,30 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
909
1082
|
print("Event data: \(eventData)")
|
|
910
1083
|
|
|
911
1084
|
DispatchQueue.main.async {
|
|
912
|
-
|
|
1085
|
+
if let targetId = call.getString("id") {
|
|
1086
|
+
guard let webViewController = self.webViewControllers[targetId] else {
|
|
1087
|
+
call.reject("WebView is not initialized")
|
|
1088
|
+
return
|
|
1089
|
+
}
|
|
1090
|
+
webViewController.postMessageToJS(message: eventData)
|
|
1091
|
+
call.resolve()
|
|
1092
|
+
return
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
if !self.webViewControllers.isEmpty {
|
|
1096
|
+
self.webViewControllers.values.forEach { $0.postMessageToJS(message: eventData) }
|
|
1097
|
+
call.resolve()
|
|
1098
|
+
return
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
guard let webViewController = self.webViewController else {
|
|
1102
|
+
call.reject("WebView is not initialized")
|
|
1103
|
+
return
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
webViewController.postMessageToJS(message: eventData)
|
|
1107
|
+
call.resolve()
|
|
913
1108
|
}
|
|
914
|
-
call.resolve()
|
|
915
1109
|
}
|
|
916
1110
|
|
|
917
1111
|
func isHexColorCode(_ input: String) -> Bool {
|
|
@@ -1020,31 +1214,45 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
1020
1214
|
let isAnimated = call.getBool("isAnimated", true)
|
|
1021
1215
|
|
|
1022
1216
|
DispatchQueue.main.async {
|
|
1023
|
-
let
|
|
1024
|
-
let
|
|
1217
|
+
let targetId = call.getString("id") ?? self.activeWebViewId
|
|
1218
|
+
if let targetId,
|
|
1219
|
+
let webViewController = self.webViewControllers[targetId],
|
|
1220
|
+
let navigationController = self.navigationControllers[targetId] {
|
|
1221
|
+
let currentUrl = webViewController.url?.absoluteString ?? ""
|
|
1222
|
+
webViewController.cleanupWebView()
|
|
1223
|
+
self.handleWebViewDidClose(id: targetId, url: currentUrl)
|
|
1224
|
+
navigationController.dismiss(animated: isAnimated, completion: nil)
|
|
1225
|
+
call.resolve()
|
|
1226
|
+
return
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
guard let webViewController = self.webViewController,
|
|
1230
|
+
let navigationController = self.navigationWebViewController else {
|
|
1231
|
+
call.reject("WebView is not initialized")
|
|
1232
|
+
return
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
let currentUrl = webViewController.url?.absoluteString ?? ""
|
|
1236
|
+
let isPresented = navigationController.presentingViewController != nil
|
|
1025
1237
|
|
|
1026
1238
|
if self.isHidden {
|
|
1027
|
-
|
|
1028
|
-
|
|
1239
|
+
webViewController.capableWebView?.removeFromSuperview()
|
|
1240
|
+
webViewController.cleanupWebView()
|
|
1029
1241
|
if isPresented {
|
|
1030
|
-
|
|
1031
|
-
self.
|
|
1032
|
-
self.navigationWebViewController = nil
|
|
1242
|
+
navigationController.dismiss(animated: isAnimated) {
|
|
1243
|
+
self.handleWebViewDidClose(id: "", url: currentUrl)
|
|
1033
1244
|
}
|
|
1034
1245
|
} else {
|
|
1035
|
-
self.
|
|
1036
|
-
self.navigationWebViewController = nil
|
|
1246
|
+
self.handleWebViewDidClose(id: "", url: currentUrl)
|
|
1037
1247
|
}
|
|
1038
1248
|
self.isHidden = false
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
self.navigationWebViewController?.dismiss(animated: isAnimated) {
|
|
1042
|
-
self.webViewController = nil
|
|
1043
|
-
self.navigationWebViewController = nil
|
|
1044
|
-
}
|
|
1249
|
+
call.resolve()
|
|
1250
|
+
return
|
|
1045
1251
|
}
|
|
1046
1252
|
|
|
1047
|
-
|
|
1253
|
+
webViewController.cleanupWebView()
|
|
1254
|
+
self.handleWebViewDidClose(id: "", url: currentUrl)
|
|
1255
|
+
navigationController.dismiss(animated: isAnimated, completion: nil)
|
|
1048
1256
|
call.resolve()
|
|
1049
1257
|
}
|
|
1050
1258
|
}
|
|
@@ -1111,7 +1319,8 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
1111
1319
|
let yPos = call.getFloat("y")
|
|
1112
1320
|
|
|
1113
1321
|
DispatchQueue.main.async {
|
|
1114
|
-
|
|
1322
|
+
let targetId = call.getString("id") ?? self.activeWebViewId
|
|
1323
|
+
guard let webViewController = self.resolveWebViewController(for: targetId) else {
|
|
1115
1324
|
call.reject("WebView is not initialized")
|
|
1116
1325
|
return
|
|
1117
1326
|
}
|
|
@@ -124,6 +124,7 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
|
|
|
124
124
|
open var cookies: [HTTPCookie]?
|
|
125
125
|
open var headers: [String: String]?
|
|
126
126
|
open var capBrowserPlugin: InAppBrowserPlugin?
|
|
127
|
+
var instanceId: String = ""
|
|
127
128
|
var shareDisclaimer: [String: Any]?
|
|
128
129
|
var shareSubject: String?
|
|
129
130
|
var didpageInit = false
|
|
@@ -518,15 +519,28 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
|
|
|
518
519
|
}
|
|
519
520
|
}
|
|
520
521
|
|
|
522
|
+
private func payload(with data: [String: Any] = [:]) -> [String: Any] {
|
|
523
|
+
guard !instanceId.isEmpty else {
|
|
524
|
+
return data
|
|
525
|
+
}
|
|
526
|
+
var payload = data
|
|
527
|
+
payload["id"] = instanceId
|
|
528
|
+
return payload
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
private func emit(_ eventName: String, data: [String: Any] = [:]) {
|
|
532
|
+
capBrowserPlugin?.notifyListeners(eventName, data: payload(with: data))
|
|
533
|
+
}
|
|
534
|
+
|
|
521
535
|
// Method to receive messages from JavaScript
|
|
522
536
|
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
523
537
|
if message.name == "messageHandler" {
|
|
524
538
|
if let messageBody = message.body as? [String: Any] {
|
|
525
539
|
print("Received message from JavaScript:", messageBody)
|
|
526
|
-
|
|
540
|
+
emit("messageFromWebview", data: messageBody)
|
|
527
541
|
} else {
|
|
528
542
|
print("Received non-dictionary message from JavaScript:", message.body)
|
|
529
|
-
|
|
543
|
+
emit("messageFromWebview", data: ["rawMessage": String(describing: message.body)])
|
|
530
544
|
}
|
|
531
545
|
} else if message.name == "preShowScriptSuccess" {
|
|
532
546
|
guard let semaphore = preShowSemaphore else {
|
|
@@ -922,7 +936,7 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
|
|
|
922
936
|
}
|
|
923
937
|
case "URL":
|
|
924
938
|
|
|
925
|
-
|
|
939
|
+
emit("urlChangeEvent", data: ["url": webView?.url?.absoluteString ?? ""])
|
|
926
940
|
self.injectJavaScriptInterface()
|
|
927
941
|
default:
|
|
928
942
|
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
|
|
@@ -967,6 +981,10 @@ public extension WKWebViewController {
|
|
|
967
981
|
webView?.reload()
|
|
968
982
|
}
|
|
969
983
|
|
|
984
|
+
func websiteDataStore() -> WKWebsiteDataStore? {
|
|
985
|
+
return webView?.configuration.websiteDataStore
|
|
986
|
+
}
|
|
987
|
+
|
|
970
988
|
func executeScript(script: String, completion: ((Any?, Error?) -> Void)? = nil) {
|
|
971
989
|
DispatchQueue.main.async { [weak self] in
|
|
972
990
|
self?.webView?.evaluateJavaScript(script, completionHandler: completion)
|
|
@@ -1297,7 +1315,7 @@ fileprivate extension WKWebViewController {
|
|
|
1297
1315
|
}
|
|
1298
1316
|
|
|
1299
1317
|
// Cannot open scheme: notify and still block WebView (avoid rendering garbage / errors)
|
|
1300
|
-
|
|
1318
|
+
emit("pageLoadError")
|
|
1301
1319
|
return true
|
|
1302
1320
|
}
|
|
1303
1321
|
|
|
@@ -1428,7 +1446,7 @@ fileprivate extension WKWebViewController {
|
|
|
1428
1446
|
}
|
|
1429
1447
|
|
|
1430
1448
|
@objc func buttonNearDoneDidClick(sender: AnyObject) {
|
|
1431
|
-
|
|
1449
|
+
emit("buttonNearDoneClick")
|
|
1432
1450
|
}
|
|
1433
1451
|
|
|
1434
1452
|
@objc func reloadDidClick(sender: AnyObject) {
|
|
@@ -1480,7 +1498,7 @@ fileprivate extension WKWebViewController {
|
|
|
1480
1498
|
style: UIAlertAction.Style.default,
|
|
1481
1499
|
handler: { _ in
|
|
1482
1500
|
// Notify that confirm was clicked
|
|
1483
|
-
self.
|
|
1501
|
+
self.emit("confirmBtnClicked", data: ["url": currentUrl])
|
|
1484
1502
|
|
|
1485
1503
|
// Show the share dialog
|
|
1486
1504
|
self.showShareSheet(items: items, sender: sender)
|
|
@@ -1520,7 +1538,7 @@ fileprivate extension WKWebViewController {
|
|
|
1520
1538
|
if canDismiss {
|
|
1521
1539
|
let currentUrl = webView?.url?.absoluteString ?? ""
|
|
1522
1540
|
cleanupWebView()
|
|
1523
|
-
self.capBrowserPlugin?.
|
|
1541
|
+
self.capBrowserPlugin?.handleWebViewDidClose(id: instanceId, url: currentUrl)
|
|
1524
1542
|
dismiss(animated: true, completion: nil)
|
|
1525
1543
|
}
|
|
1526
1544
|
}
|
|
@@ -1532,7 +1550,7 @@ fileprivate extension WKWebViewController {
|
|
|
1532
1550
|
let alert = UIAlertController(title: self.closeModalTitle, message: self.closeModalDescription, preferredStyle: UIAlertController.Style.alert)
|
|
1533
1551
|
alert.addAction(UIAlertAction(title: self.closeModalOk, style: UIAlertAction.Style.default, handler: { _ in
|
|
1534
1552
|
// Notify that confirm was clicked
|
|
1535
|
-
self.
|
|
1553
|
+
self.emit("confirmBtnClicked", data: ["url": currentUrl])
|
|
1536
1554
|
self.closeView()
|
|
1537
1555
|
}))
|
|
1538
1556
|
alert.addAction(UIAlertAction(title: self.closeModalCancel, style: UIAlertAction.Style.default, handler: nil))
|
|
@@ -1552,7 +1570,7 @@ fileprivate extension WKWebViewController {
|
|
|
1552
1570
|
func close() {
|
|
1553
1571
|
let currentUrl = webView?.url?.absoluteString ?? ""
|
|
1554
1572
|
cleanupWebView()
|
|
1555
|
-
capBrowserPlugin?.
|
|
1573
|
+
capBrowserPlugin?.handleWebViewDidClose(id: instanceId, url: currentUrl)
|
|
1556
1574
|
dismiss(animated: true, completion: nil)
|
|
1557
1575
|
}
|
|
1558
1576
|
|
|
@@ -1805,11 +1823,11 @@ extension WKWebViewController: WKNavigationDelegate {
|
|
|
1805
1823
|
DispatchQueue.global(qos: .userInitiated).async {
|
|
1806
1824
|
self.injectPreShowScript()
|
|
1807
1825
|
DispatchQueue.main.async { [weak self] in
|
|
1808
|
-
self?.capBrowserPlugin?.presentView()
|
|
1826
|
+
self?.capBrowserPlugin?.presentView(webViewId: self?.instanceId)
|
|
1809
1827
|
}
|
|
1810
1828
|
}
|
|
1811
1829
|
} else {
|
|
1812
|
-
self.capBrowserPlugin?.presentView()
|
|
1830
|
+
self.capBrowserPlugin?.presentView(webViewId: instanceId)
|
|
1813
1831
|
}
|
|
1814
1832
|
} else if self.preShowScript != nil &&
|
|
1815
1833
|
!self.preShowScript!.isEmpty &&
|
|
@@ -1834,7 +1852,7 @@ extension WKWebViewController: WKNavigationDelegate {
|
|
|
1834
1852
|
delegate?.webViewController?(self, didFinish: url)
|
|
1835
1853
|
}
|
|
1836
1854
|
self.injectJavaScriptInterface()
|
|
1837
|
-
|
|
1855
|
+
emit("browserPageLoaded")
|
|
1838
1856
|
}
|
|
1839
1857
|
|
|
1840
1858
|
public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
|
|
@@ -1844,7 +1862,7 @@ extension WKWebViewController: WKNavigationDelegate {
|
|
|
1844
1862
|
self.url = url
|
|
1845
1863
|
delegate?.webViewController?(self, didFail: url, withError: error)
|
|
1846
1864
|
}
|
|
1847
|
-
|
|
1865
|
+
emit("pageLoadError")
|
|
1848
1866
|
}
|
|
1849
1867
|
|
|
1850
1868
|
public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
|
|
@@ -1854,7 +1872,7 @@ extension WKWebViewController: WKNavigationDelegate {
|
|
|
1854
1872
|
self.url = url
|
|
1855
1873
|
delegate?.webViewController?(self, didFail: url, withError: error)
|
|
1856
1874
|
}
|
|
1857
|
-
|
|
1875
|
+
emit("pageLoadError")
|
|
1858
1876
|
}
|
|
1859
1877
|
|
|
1860
1878
|
public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
|
|
@@ -1929,7 +1947,7 @@ extension WKWebViewController: WKNavigationDelegate {
|
|
|
1929
1947
|
|
|
1930
1948
|
if self.shouldBlockHost(host) {
|
|
1931
1949
|
print("[InAppBrowser] Blocked host detected: \(host)")
|
|
1932
|
-
|
|
1950
|
+
emit("urlChangeEvent", data: ["url": url.absoluteString])
|
|
1933
1951
|
decisionHandler(.cancel)
|
|
1934
1952
|
return
|
|
1935
1953
|
}
|