diff --git a/CountlyContentBuilderInternal.m b/CountlyContentBuilderInternal.m index 9af91e28..59589114 100644 --- a/CountlyContentBuilderInternal.m +++ b/CountlyContentBuilderInternal.m @@ -102,9 +102,23 @@ - (void)fetchContents { return; } - NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; - NSString *pathToHtml = jsonResponse[@"pathToHtml"]; - NSDictionary *placementCoordinates = jsonResponse[@"placementCoordinates"]; + NSError *jsonError; + NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError]; + + if (jsonError) { + CLY_LOG_I(@"Failed to parse JSON: %@", jsonError); + self->_isRequestQueueLocked = NO; + return; + } + + if (!jsonResponse) { + CLY_LOG_I(@"Received empty or null response."); + self->_isRequestQueueLocked = NO; + return; + } + + NSString *pathToHtml = jsonResponse[@"html"]; + NSDictionary *placementCoordinates = jsonResponse[@"geo"]; if(pathToHtml) { [self showContentWithHtmlPath:pathToHtml placementCoordinates:placementCoordinates]; } @@ -118,9 +132,8 @@ - (NSURLRequest *)fetchContentsRequest { NSString* queryString = [CountlyConnectionManager.sharedInstance queryEssentials]; NSString *resolutionJson = [self resolutionJson]; - queryString = [queryString stringByAppendingFormat:@"&%@=%@&%@=%@", - kCountlyQSKeyMethod, kCountlyCBFetchContent, - @"res", resolutionJson]; + queryString = [queryString stringByAppendingFormat:@"&%@=%@", + @"resolution", resolutionJson]; queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString]; @@ -155,8 +168,8 @@ - (NSString *)resolutionJson { CGFloat height = screenBounds.size.height; NSDictionary *resolutionDict = @{ - @"p": @{@"h": @(height), @"w": @(width)}, - @"l": @{@"h": @(width), @"w": @(height)} + @"portrait": @{@"height": @(height), @"width": @(width)}, + @"landscape": @{@"height": @(width), @"width": @(height)} }; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resolutionDict options:0 error:nil]; @@ -180,12 +193,12 @@ - (void)showContentWithHtmlPath:(NSString *)urlString placementCoordinates:(NSDi // Get the appropriate coordinates based on the orientation - NSDictionary *coordinates = isLandscape ? placementCoordinates[@"landscape"] : placementCoordinates[@"portrait"]; + NSDictionary *coordinates = isLandscape ? placementCoordinates[@"l"] : placementCoordinates[@"p"]; CGFloat x = [coordinates[@"x"] floatValue]; CGFloat y = [coordinates[@"y"] floatValue]; - CGFloat width = [coordinates[@"width"] floatValue]; - CGFloat height = [coordinates[@"height"] floatValue]; + CGFloat width = [coordinates[@"w"] floatValue]; + CGFloat height = [coordinates[@"h"] floatValue]; CGRect frame = CGRectMake(x, y, width, height); diff --git a/CountlyPersistency.m b/CountlyPersistency.m index 276f70db..5edff471 100644 --- a/CountlyPersistency.m +++ b/CountlyPersistency.m @@ -282,23 +282,27 @@ - (void)recordEvent:(CountlyEvent *)event - (NSString *)serializedRecordedEvents { - NSMutableArray* tempArray = NSMutableArray.new; - + NSMutableArray *tempArray = NSMutableArray.new; + @synchronized (self.recordedEvents) { if (self.recordedEvents.count == 0) return nil; - - for (CountlyEvent* event in self.recordedEvents.copy) + + NSArray *eventsCopy = self.recordedEvents.copy; + + for (CountlyEvent *event in eventsCopy) { [tempArray addObject:[event dictionaryRepresentation]]; - [self.recordedEvents removeObject:event]; } + + [self.recordedEvents removeObjectsInArray:eventsCopy]; } - + return [tempArray cly_JSONify]; } + - (void)flushEvents { @synchronized (self.recordedEvents)