From 84f77847a8b6f421057ae3599d68076c894ee8a4 Mon Sep 17 00:00:00 2001 From: bahricanyesil Date: Wed, 5 Mar 2025 17:53:37 +0300 Subject: [PATCH] fix: add nil check before serializing --- ios/Classes/AdjustSdk.m | 24 +++++++++++++++--------- ios/Classes/AdjustSdkDelegate.m | 24 +++++++++++++++--------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/ios/Classes/AdjustSdk.m b/ios/Classes/AdjustSdk.m index 37814fc..3a3c395 100644 --- a/ios/Classes/AdjustSdk.m +++ b/ios/Classes/AdjustSdk.m @@ -267,7 +267,7 @@ - (void)initSdk:(FlutterMethodCall *)call withResult:(FlutterResult)result { [adjustConfig disableIdfvReading]; } } - + // SKAdNetwork attribution if ([self isFieldValid:isSkanAttributionEnabled]) { if ([isSkanAttributionEnabled boolValue] == NO) { @@ -419,7 +419,7 @@ - (void)processDeeplink:(FlutterMethodCall *)call withResult:(FlutterResult)resu if (urlString == nil) { return; } - + NSURL *url = [NSURL URLWithString:urlString]; ADJDeeplink *deeplink = [[ADJDeeplink alloc] initWithDeeplink:url]; [Adjust processDeeplink:deeplink]; @@ -456,12 +456,12 @@ - (void)trackAdRevenue:(FlutterMethodCall *)call withResult:(FlutterResult)resul if ([self isFieldValid:adRevenueNetwork]) { [adjustAdRevenue setAdRevenueNetwork:adRevenueNetwork]; } - + // ad revenue unit if ([self isFieldValid:adRevenueUnit]) { [adjustAdRevenue setAdRevenueUnit:adRevenueUnit]; } - + // ad revenue placement if ([self isFieldValid:adRevenuePlacement]) { [adjustAdRevenue setAdRevenuePlacement:adRevenuePlacement]; @@ -576,13 +576,19 @@ - (void)getAttribution:(FlutterMethodCall *)call withResult:(FlutterResult)resul [self addValueOrEmpty:attribution.costType withKey:@"costType" toDictionary:dictionary]; [self addNumberOrEmpty:attribution.costAmount withKey:@"costAmount" toDictionary:dictionary]; [self addValueOrEmpty:attribution.costCurrency withKey:@"costCurrency" toDictionary:dictionary]; - NSData *dataJsonResponse = [NSJSONSerialization dataWithJSONObject:attribution.jsonResponse + + // Add nil check before serializing jsonResponse + if (attribution.jsonResponse != nil) { + NSData *dataJsonResponse = [NSJSONSerialization dataWithJSONObject:attribution.jsonResponse options:0 error:nil]; - NSString *stringJsonResponse = [[NSString alloc] initWithBytes:[dataJsonResponse bytes] + NSString *stringJsonResponse = [[NSString alloc] initWithBytes:[dataJsonResponse bytes] length:[dataJsonResponse length] encoding:NSUTF8StringEncoding]; - [self addValueOrEmpty:stringJsonResponse withKey:@"jsonResponse" toDictionary:dictionary]; + [self addValueOrEmpty:stringJsonResponse withKey:@"jsonResponse" toDictionary:dictionary]; + } else { + [self addValueOrEmpty:@"" withKey:@"jsonResponse" toDictionary:dictionary]; + } result(dictionary); }]; } @@ -687,7 +693,7 @@ - (void)verifyAppStorePurchase:(FlutterMethodCall *)call withResult:(FlutterResu if (verificationResult == nil) { result(dictionary); } - + [self addValueOrEmpty:verificationResult.verificationStatus withKey:@"verificationStatus" toDictionary:dictionary]; @@ -788,7 +794,7 @@ - (void)verifyAndTrackAppStorePurchase:(FlutterMethodCall *)call withResult:(Flu if (verificationResult == nil) { result(dictionary); } - + [self addValueOrEmpty:verificationResult.verificationStatus withKey:@"verificationStatus" toDictionary:dictionary]; diff --git a/ios/Classes/AdjustSdkDelegate.m b/ios/Classes/AdjustSdkDelegate.m index 49ae65d..d53ef5a 100644 --- a/ios/Classes/AdjustSdkDelegate.m +++ b/ios/Classes/AdjustSdkDelegate.m @@ -44,7 +44,7 @@ + (id)getInstanceWithSwizzleOfAttributionCallback:(NSString *)swizzleAttribution methodChannel:(FlutterMethodChannel *)channel { dispatch_once(&onceToken, ^{ defaultInstance = [[AdjustSdkDelegate alloc] init]; - + // do the swizzling where and if needed if (swizzleAttributionCallback != nil) { [defaultInstance swizzleCallbackMethod:@selector(adjustAttributionChanged:) @@ -107,7 +107,7 @@ - (void)adjustAttributionChangedWannabe:(ADJAttribution *)attribution { if (nil == attribution || nil == dartAttributionCallback) { return; } - + id keys[] = { @"trackerToken", @"trackerName", @@ -121,12 +121,18 @@ - (void)adjustAttributionChangedWannabe:(ADJAttribution *)attribution { @"costCurrency", @"jsonResponse" }; - NSData *dataJsonResponse = [NSJSONSerialization dataWithJSONObject:attribution.jsonResponse - options:0 - error:nil]; - NSString *stringJsonResponse = [[NSString alloc] initWithBytes:[dataJsonResponse bytes] - length:[dataJsonResponse length] - encoding:NSUTF8StringEncoding]; + + // Add nil check before serializing jsonResponse + NSString *stringJsonResponse = @""; + if (attribution.jsonResponse != nil) { + NSData *dataJsonResponse = [NSJSONSerialization dataWithJSONObject:attribution.jsonResponse + options:0 + error:nil]; + stringJsonResponse = [[NSString alloc] initWithBytes:[dataJsonResponse bytes] + length:[dataJsonResponse length] + encoding:NSUTF8StringEncoding]; + } + id values[] = { [self getValueOrEmpty:[attribution trackerToken]], [self getValueOrEmpty:[attribution trackerName]], @@ -202,7 +208,7 @@ - (void)adjustEventTrackingSucceededWannabe:(ADJEventSuccess *)eventSuccessRespo if (nil == eventSuccessResponseData || nil == dartEventSuccessCallback) { return; } - + id keys[] = { @"message", @"timestamp",