@@ -96,16 +96,23 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
96
96
if ([url hasPrefix: @" https://countly_action_event" ] && [url containsString: @" cly_x_action_event=1" ]) {
97
97
NSDictionary *queryParameters = [self parseQueryString: url];
98
98
NSString *action = queryParameters[@" action" ];
99
-
100
- if ([action isEqualToString: @" event" ]) {
101
- NSString *eventsJson = queryParameters[@" event" ];
102
- [self recordEventsWithJSONString: eventsJson];
103
- } else if ([action isEqualToString: @" link" ]) {
104
- NSString *link = queryParameters[@" link" ];
105
- [self openExternalLink: link ];
106
- } else if ([action isEqualToString: @" resize_me" ]) {
107
- NSString *resize = queryParameters[@" resize_me" ];
108
- [self resizeWebViewWithJSONString: resize];
99
+ if (action) {
100
+ if ([action isEqualToString: @" event" ]) {
101
+ NSString *eventsJson = queryParameters[@" event" ];
102
+ if (eventsJson) {
103
+ [self recordEventsWithJSONString: eventsJson];
104
+ }
105
+ } else if ([action isEqualToString: @" link" ]) {
106
+ NSString *link = queryParameters[@" link" ];
107
+ if (link ) {
108
+ [self openExternalLink: link ];
109
+ }
110
+ } else if ([action isEqualToString: @" resize_me" ]) {
111
+ NSString *resize = queryParameters[@" resize_me" ];
112
+ if (resize) {
113
+ [self resizeWebViewWithJSONString: resize];
114
+ }
115
+ }
109
116
}
110
117
111
118
if ([queryParameters[@" close" ] boolValue ]) {
@@ -182,12 +189,30 @@ - (NSDictionary *)parseQueryString:(NSString *)url {
182
189
}
183
190
184
191
- (void )recordEventsWithJSONString : (NSString *)jsonString {
185
- NSData *data = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
186
- NSArray *events = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil ];
192
+ // Decode the URL-encoded JSON string
193
+ NSString *decodedString = [jsonString stringByRemovingPercentEncoding ];
194
+
195
+ // Convert the decoded string to NSData
196
+ NSData *data = [decodedString dataUsingEncoding: NSUTF8StringEncoding];
197
+
198
+ // Parse the JSON data
199
+ NSError *error = nil ;
200
+ NSArray *events = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &error];
201
+
202
+ if (error) {
203
+ NSLog (@" Error parsing JSON: %@ " , error);
204
+ } else {
205
+ NSLog (@" Parsed JSON: %@ " , events);
206
+ }
207
+
187
208
188
209
for (NSDictionary *event in events) {
189
210
NSString *key = event[@" key" ];
190
211
NSDictionary *segmentation = event[@" sg" ];
212
+ if (!key) {
213
+ CLY_LOG_I (@" Skipping the event due to key is empty or nil" );
214
+ continue ;
215
+ }
191
216
if (!segmentation) {
192
217
CLY_LOG_I (@" Skipping the event due to missing segmentation" );
193
218
continue ;
@@ -211,22 +236,55 @@ - (void)openExternalLink:(NSString *)urlString {
211
236
}
212
237
213
238
- (void )resizeWebViewWithJSONString : (NSString *)jsonString {
214
- NSData *data = [jsonString dataUsingEncoding: NSUTF8StringEncoding];
215
- NSDictionary *resizeDict = [NSJSONSerialization JSONObjectWithData: data options: 0 error: nil ];
216
239
240
+ // Decode the URL-encoded JSON string
241
+ NSString *decodedString = [jsonString stringByRemovingPercentEncoding ];
242
+
243
+ // Convert the decoded string to NSData
244
+ NSData *data = [decodedString dataUsingEncoding: NSUTF8StringEncoding];
245
+
246
+ // Parse the JSON data
247
+ NSError *error = nil ;
248
+ NSDictionary *resizeDict = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &error];
249
+
250
+ if (!resizeDict) {
251
+ CLY_LOG_I (@" Resize dictionary should not be empty or nil. Error: %@ " , error);
252
+ return ;
253
+ }
254
+
255
+ // Ensure resizeDict is a dictionary
256
+ if (![resizeDict isKindOfClass: [NSDictionary class ]]) {
257
+ CLY_LOG_I (@" Resize dictionary should be of type NSDictionary" );
258
+ return ;
259
+ }
260
+
261
+ // Retrieve portrait and landscape dimensions
217
262
NSDictionary *portraitDimensions = resizeDict[@" p" ];
218
263
NSDictionary *landscapeDimensions = resizeDict[@" l" ];
219
264
265
+ if (!portraitDimensions && !landscapeDimensions) {
266
+ CLY_LOG_I (@" Resize dimensions should not be empty or nil" );
267
+ return ;
268
+ }
269
+
270
+ // Determine the current orientation
220
271
UIInterfaceOrientation orientation = [UIApplication sharedApplication ].statusBarOrientation ;
221
272
BOOL isLandscape = UIInterfaceOrientationIsLandscape (orientation);
222
273
274
+ // Select the appropriate dimensions based on orientation
223
275
NSDictionary *dimensions = isLandscape ? landscapeDimensions : portraitDimensions;
224
276
277
+ // Get the dimension values
278
+ CGFloat x = [dimensions[@" x" ] floatValue ];
279
+ CGFloat y = [dimensions[@" y" ] floatValue ];
225
280
CGFloat width = [dimensions[@" w" ] floatValue ];
226
281
CGFloat height = [dimensions[@" h" ] floatValue ];
227
282
283
+ // Animate the resizing of the web view
228
284
[UIView animateWithDuration: 0.3 animations: ^{
229
285
CGRect frame = self.backgroundView .webView .frame ;
286
+ frame.origin .x = x;
287
+ frame.origin .y = y;
230
288
frame.size .width = width;
231
289
frame.size .height = height;
232
290
self.backgroundView .webView .frame = frame;
@@ -236,6 +294,7 @@ - (void)resizeWebViewWithJSONString:(NSString *)jsonString {
236
294
}
237
295
238
296
297
+
239
298
- (void )closeWebView {
240
299
dispatch_async (dispatch_get_main_queue (), ^{
241
300
if (self.dismissBlock ) {
0 commit comments