cordova-plugin-mserviceinterface-2.0 1.0.100 → 1.0.101

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