@@ -30,44 +30,12 @@ public final class WebViewController: UIViewController {
3030 let userContentController = WKUserContentController ( )
3131 let userDefaults = UserDefaults . standard
3232
33- if customConfig != nil {
34- let preloadScript = preloadConsentDataScript ( )
35- userContentController. addUserScript (
36- WKUserScript (
37- source: preloadScript,
38- injectionTime: . atDocumentStart,
39- forMainFrameOnly: false ) )
40- } else {
41- let fullBridge = """
42- (function() {
43- window.clickioSDK = window.clickioSDK || {};
44- const originalWrite = window.clickioSDK.write;
45- const originalRead = window.clickioSDK.read;
46- const originalReady = window.clickioSDK.ready;
47-
48- window.clickioSDK.write = function(data) {
49- window.webkit.messageHandlers.clickioSDK.postMessage({ action: 'write', data });
50- if (originalWrite) originalWrite.apply(this, arguments);
51- };
52-
53- window.clickioSDK.read = function(key) {
54- window.webkit.messageHandlers.clickioSDK.postMessage({ action: 'read', data: key });
55- if (originalRead) originalRead.apply(this, arguments);
56- };
57-
58- window.clickioSDK.ready = function() {
59- window.webkit.messageHandlers.clickioSDK.postMessage({ action: 'ready' });
60- if (originalReady) originalReady.apply(this, arguments);
61- };
62- })();
63- """
64-
65- userContentController. addUserScript (
66- WKUserScript (
67- source: fullBridge,
33+ let preloadScript = preloadConsentDataScript ( )
34+ userContentController. addUserScript (
35+ WKUserScript (
36+ source: preloadScript,
6837 injectionTime: . atDocumentStart,
6938 forMainFrameOnly: false ) )
70- }
7139
7240 // Initialize the webView with the configuration
7341 webConfiguration. userContentController = userContentController
@@ -165,15 +133,10 @@ public final class WebViewController: UIViewController {
165133 if let n = any as? NSNumber { return n }
166134 if any is NSNull { return NSNull ( ) }
167135
168- if let data = any as? Data {
169- return data. base64EncodedString ( )
170- }
171- if let url = any as? URL {
172- return url. absoluteString
173- }
174- if let date = any as? Date {
175- return ISO8601DateFormatter ( ) . string ( from: date)
176- }
136+ if let data = any as? Data { return data. base64EncodedString ( ) }
137+ if let url = any as? URL { return url. absoluteString }
138+ if let date = any as? Date { return ISO8601DateFormatter ( ) . string ( from: date) }
139+
177140 if let arr = any as? [ Any ] {
178141 return arr. compactMap { makeJSONCompatible ( $0) }
179142 }
@@ -186,6 +149,7 @@ public final class WebViewController: UIViewController {
186149 }
187150 return out
188151 }
152+
189153 return String ( describing: any)
190154 }
191155
@@ -207,7 +171,12 @@ public final class WebViewController: UIViewController {
207171 jsonData = try JSONSerialization . data ( withJSONObject: safe, options: [ ] )
208172 } catch {
209173 logger. log ( " Failed to create JSON for preload: \( error. localizedDescription) " , level: . error)
210- jsonData = " {} " . data ( using: . utf8) !
174+ return """
175+ (function() {
176+ window.clickioSDK = window.clickioSDK || {};
177+ window.clickioSDK.preloaded = {};
178+ })();
179+ """
211180 }
212181
213182 let jsonString = String ( data: jsonData, encoding: . utf8) ?? " {} "
@@ -231,14 +200,19 @@ public final class WebViewController: UIViewController {
231200 window.clickioSDK.read = function(key) {
232201 window.webkit.messageHandlers.clickioSDK.postMessage({ action: 'read', data: key });
233202
234- if (window.clickioSDK.preloaded && key in window.clickioSDK.preloaded) {
203+ if (window.clickioSDK.preloaded && Object.prototype.hasOwnProperty.call( window.clickioSDK.preloaded, key) ) {
235204 return window.clickioSDK.preloaded[key];
236205 }
237206
238207 if (originalRead) return originalRead.apply(this, arguments);
239-
240208 return null;
241209 };
210+
211+ // Ready
212+ window.clickioSDK.ready = function() {
213+ window.webkit.messageHandlers.clickioSDK.postMessage({ action: 'ready' });
214+ if (originalReady) originalReady.apply(this, arguments);
215+ };
242216 })();
243217 """
244218 }
@@ -284,16 +258,12 @@ extension WebViewController: WKScriptMessageHandler {
284258
285259 private func handleReadAction( key: String ) {
286260 logger. log ( " Read method was called with key: \( key) " , level: . info)
287-
261+
288262 let userDefaults = UserDefaults . standard
289263 if let value = userDefaults. value ( forKey: key) {
290264 logger. log ( " Value for key ' \( key) ': \( value) " , level: . debug)
291- let script = " window.clickioSDK.onRead(' \( key) ', \( value) ); "
292- webView. evaluateJavaScript ( script, completionHandler: nil )
293265 } else {
294266 logger. log ( " No value found for key ' \( key) ' " , level: . debug)
295- let script = " window.clickioSDK.onRead(' \( key) ', null); "
296- webView. evaluateJavaScript ( script, completionHandler: nil )
297267 }
298268 }
299269
0 commit comments