cordova-plugin-mserviceinterface-2.0 1.0.100 → 1.0.102

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cordova-plugin-mserviceinterface-2.0",
3
- "version": "1.0.100",
3
+ "version": "1.0.102",
4
4
  "description": "This plugin establishes the connection to the native interface and performs the requested task.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/plugin.xml CHANGED
@@ -1,6 +1,6 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
3
- xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-mserviceinterface-2.0" version="1.0.100">
3
+ xmlns:android="http://schemas.android.com/apk/res/android" id="cordova-plugin-mserviceinterface-2.0" version="1.0.102">
4
4
  <name>mserviceinterface-2.0</name>
5
5
  <description>This plugin establishes the connection to the native interface and performs the requested task.</description>
6
6
  <author>selfservit@gmail.com</author>
@@ -8,12 +8,13 @@
8
8
 
9
9
  import UIKit
10
10
  import Cordova
11
+ import WebKit
11
12
 
12
13
  class SceneDelegate: CDVSceneDelegate {
13
14
 
14
15
  private var pendingDeepLinkURL: String?
15
16
  private var deepLinkRetryCount: Int = 0
16
- private let maxDeepLinkRetryCount: Int = 30
17
+ private let maxDeepLinkRetryCount: Int = 80
17
18
 
18
19
  /*
19
20
  * Cold-start handling:
@@ -30,9 +31,22 @@ class SceneDelegate: CDVSceneDelegate {
30
31
  options: connectionOptions
31
32
  )
32
33
 
33
- if let userActivity = connectionOptions.userActivities.first,
34
- userActivity.activityType == NSUserActivityTypeBrowsingWeb,
35
- let url = userActivity.webpageURL {
34
+ if let userActivity =
35
+ connectionOptions.userActivities.first(
36
+ where: {
37
+ $0.activityType ==
38
+ NSUserActivityTypeBrowsingWeb &&
39
+ $0.webpageURL != nil
40
+ }
41
+ ),
42
+ let url = userActivity.webpageURL {
43
+
44
+ print(
45
+ "[MService Deep Link] " +
46
+ "Cold-start Universal Link: " +
47
+ url.absoluteString
48
+ )
49
+
36
50
  self.handleDeepLink(url)
37
51
  return
38
52
  }
@@ -94,62 +108,148 @@ class SceneDelegate: CDVSceneDelegate {
94
108
  }
95
109
 
96
110
  private func sendPendingDeepLinkToJavaScript() {
97
- guard
98
- let urlString = self.pendingDeepLinkURL,
99
- !urlString.isEmpty
100
- else {
101
- return
102
- }
111
+ guard
112
+ let urlString = self.pendingDeepLinkURL,
113
+ !urlString.isEmpty
114
+ else {
115
+ return
116
+ }
103
117
 
104
- let quotedURL = self.getJavaScriptString(
105
- urlString
106
- )
118
+ let quotedURL = self.getJavaScriptString(
119
+ urlString
120
+ )
107
121
 
108
- let javaScript = """
122
+ /*
123
+ * The JavaScript returns a status to native code.
124
+ *
125
+ * PROCESSED:
126
+ * window.handleOpenURL exists and has received the URL.
127
+ *
128
+ * WAITING_FOR_HANDLER:
129
+ * Cordova page exists, but application libraries are not ready.
130
+ *
131
+ * ERROR:
132
+ * JavaScript execution failed and must be retried.
133
+ */
134
+ let javaScript = """
135
+ (function () {
109
136
  try {
110
- window.mServicePendingUniversalLink = \(quotedURL);
137
+ window.mServicePendingUniversalLink =
138
+ \(quotedURL);
111
139
 
112
140
  window.localStorage.setItem(
113
141
  "mServicePendingUniversalLink",
114
142
  \(quotedURL)
115
143
  );
116
144
 
117
- if (window.handleOpenURL) {
118
- window.handleOpenURL(\(quotedURL));
119
-
120
- window.mServicePendingUniversalLink = "";
145
+ if (
146
+ typeof window.handleOpenURL !==
147
+ "function"
148
+ ) {
149
+ return "WAITING_FOR_HANDLER";
150
+ }
121
151
 
122
- window.localStorage.removeItem(
123
- "mServicePendingUniversalLink"
152
+ var handleResult =
153
+ window.handleOpenURL(
154
+ \(quotedURL)
124
155
  );
156
+
157
+ /*
158
+ * A handler may explicitly return false when
159
+ * the application is not ready.
160
+ */
161
+ if (handleResult === false) {
162
+ return "HANDLER_NOT_READY";
125
163
  }
164
+
165
+ window.mServicePendingUniversalLink = "";
166
+
167
+ window.localStorage.removeItem(
168
+ "mServicePendingUniversalLink"
169
+ );
170
+
171
+ return "PROCESSED";
126
172
  } catch (exception) {
127
173
  console.log(
174
+ "[MService Deep Link] " +
128
175
  "Universal Link JavaScript error: " +
129
176
  exception
130
177
  );
131
- }
132
- """
133
178
 
134
- DispatchQueue.main.async { [weak self] in
135
- guard let self = self else {
136
- return
179
+ return "ERROR";
137
180
  }
181
+ }());
182
+ """
138
183
 
139
- if let viewController =
140
- self.getCordovaViewController() {
184
+ DispatchQueue.main.async { [weak self] in
185
+ guard let self = self else {
186
+ return
187
+ }
141
188
 
142
- viewController.commandDelegate.evalJs(
143
- javaScript
144
- )
189
+ guard let viewController =
190
+ self.getCordovaViewController()
191
+ else {
192
+ self.retrySendingDeepLink()
193
+ return
194
+ }
145
195
 
146
- self.pendingDeepLinkURL = nil
147
- self.deepLinkRetryCount = 0
148
- } else {
149
- self.retrySendingDeepLink()
150
- }
196
+ /*
197
+ * Do not use commandDelegate.evalJs here.
198
+ *
199
+ * evaluateJavaScript provides a completion handler,
200
+ * allowing native code to confirm whether the app-side
201
+ * handler was actually available.
202
+ */
203
+ guard let webView =
204
+ viewController.webViewEngine.engineWebView as? WKWebView
205
+ else {
206
+ print(
207
+ "[MService Deep Link] " +
208
+ "WKWebView is not ready"
209
+ )
210
+
211
+ self.retrySendingDeepLink()
212
+ return
213
+ }
214
+
215
+ webView.evaluateJavaScript(
216
+ javaScript
217
+ ) { [weak self] result, error in
218
+
219
+ DispatchQueue.main.async {
220
+ guard let self = self else {
221
+ return
151
222
  }
223
+
224
+ if let error = error {
225
+ print(
226
+ "[MService Deep Link] " +
227
+ "JavaScript execution failed: " +
228
+ error.localizedDescription
229
+ )
230
+
231
+ self.retrySendingDeepLink()
232
+ return
233
+ }
234
+
235
+ let status =
236
+ result as? String ?? ""
237
+
238
+ print(
239
+ "[MService Deep Link] " +
240
+ "JavaScript status: \(status)"
241
+ )
242
+
243
+ if status == "PROCESSED" {
244
+ self.pendingDeepLinkURL = nil
245
+ self.deepLinkRetryCount = 0
246
+ } else {
247
+ self.retrySendingDeepLink()
248
+ }
249
+ }
250
+ }
152
251
  }
252
+ }
153
253
 
154
254
  private func retrySendingDeepLink() {
155
255
  if self.deepLinkRetryCount <
@@ -105,4 +105,11 @@
105
105
  },
106
106
  false
107
107
  );
108
+ window.addEventListener(
109
+ "pageshow",
110
+ function () {
111
+ processPendingURL();
112
+ },
113
+ false
114
+ );
108
115
  }());