Skip to content

Commit 98f8f43

Browse files
authored
Merge pull request #352 from Countly/content-improvements
Added additional checks for content after testing
2 parents 9f9fb38 + 021236f commit 98f8f43

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## xx.xx.xx
2+
* Mitigated an issue with content action json parsing due to json encoding
23
* Mitigated an issue where pausing a view resulted in a '0' view duration.
34
* Mitigated an issue where an internal timer was not reset when going to foreground for `autoStoppedViews`
45
* Mitigated an issue for `autoStoppedViews` could have not started when multiple views were open at the same time while going to foreground

CountlyWebViewManager.m

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,23 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
9696
if ([url hasPrefix:@"https://countly_action_event"] && [url containsString:@"cly_x_action_event=1"]) {
9797
NSDictionary *queryParameters = [self parseQueryString:url];
9898
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+
}
109116
}
110117

111118
if ([queryParameters[@"close"] boolValue]) {
@@ -182,12 +189,30 @@ - (NSDictionary *)parseQueryString:(NSString *)url {
182189
}
183190

184191
- (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+
187208

188209
for (NSDictionary *event in events) {
189210
NSString *key = event[@"key"];
190211
NSDictionary *segmentation = event[@"sg"];
212+
if(!key) {
213+
CLY_LOG_I(@"Skipping the event due to key is empty or nil");
214+
continue;
215+
}
191216
if(!segmentation) {
192217
CLY_LOG_I(@"Skipping the event due to missing segmentation");
193218
continue;
@@ -211,22 +236,55 @@ - (void)openExternalLink:(NSString *)urlString {
211236
}
212237

213238
- (void)resizeWebViewWithJSONString:(NSString *)jsonString {
214-
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
215-
NSDictionary *resizeDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
216239

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
217262
NSDictionary *portraitDimensions = resizeDict[@"p"];
218263
NSDictionary *landscapeDimensions = resizeDict[@"l"];
219264

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
220271
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
221272
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
222273

274+
// Select the appropriate dimensions based on orientation
223275
NSDictionary *dimensions = isLandscape ? landscapeDimensions : portraitDimensions;
224276

277+
// Get the dimension values
278+
CGFloat x = [dimensions[@"x"] floatValue];
279+
CGFloat y = [dimensions[@"y"] floatValue];
225280
CGFloat width = [dimensions[@"w"] floatValue];
226281
CGFloat height = [dimensions[@"h"] floatValue];
227282

283+
// Animate the resizing of the web view
228284
[UIView animateWithDuration:0.3 animations:^{
229285
CGRect frame = self.backgroundView.webView.frame;
286+
frame.origin.x = x;
287+
frame.origin.y = y;
230288
frame.size.width = width;
231289
frame.size.height = height;
232290
self.backgroundView.webView.frame = frame;
@@ -236,6 +294,7 @@ - (void)resizeWebViewWithJSONString:(NSString *)jsonString {
236294
}
237295

238296

297+
239298
- (void)closeWebView {
240299
dispatch_async(dispatch_get_main_queue(), ^{
241300
if (self.dismissBlock) {

0 commit comments

Comments
 (0)