Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set default settings is segment.com can't be reached. #888

Merged
merged 12 commits into from
May 20, 2020
6 changes: 6 additions & 0 deletions Analytics/Classes/Integrations/SEGIntegrationsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ - (void)refreshSettings
NSDictionary *previouslyCachedSettings = [self cachedSettings];
if (previouslyCachedSettings) {
[self setCachedSettings:previouslyCachedSettings];
} else if (self.configuration.defaultSettings != nil) {
// If settings request fail, load a user-supplied version if present.
// but make sure segment.io is in the integrations
NSMutableDictionary *newSettings = [self.configuration.defaultSettings serializableMutableDeepCopy];
newSettings[@"integrations"][@"Segment.io"][@"apiKey"] = self.configuration.writeKey;
[self setCachedSettings:newSettings];
} else {
// If settings request fail, fall back to using just Segment integration.
// Doesn't address situations where this callback never gets called (though we don't expect that to ever happen).
Expand Down
1 change: 1 addition & 0 deletions Analytics/Classes/Internal/SEGAnalyticsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NSString *SEGEventNameForScreenTitle(NSString *title);

// Deep copy and check NSCoding conformance
@protocol SEGSerializableDeepCopy <NSObject>
-(id _Nullable) serializableMutableDeepCopy;
-(id _Nullable) serializableDeepCopy;
@end

Expand Down
48 changes: 37 additions & 11 deletions Analytics/Classes/Internal/SEGAnalyticsUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ static id SEGCoerceJSONObject(id obj)

@implementation NSDictionary(SerializableDeepCopy)

- (NSDictionary *)serializableDeepCopy
- (id)serializableDeepCopy:(BOOL)mutable
{
NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] initWithCapacity:self.count];
NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithCapacity:self.count];
NSArray *keys = [self allKeys];
for (id key in keys) {
id aValue = [self objectForKey:key];
Expand All @@ -263,27 +263,39 @@ - (NSDictionary *)serializableDeepCopy
}

if ([aValue conformsToProtocol:@protocol(SEGSerializableDeepCopy)]) {
theCopy = [aValue serializableDeepCopy];
theCopy = [aValue serializableDeepCopy:mutable];
} else if ([aValue conformsToProtocol:@protocol(NSCopying)]) {
theCopy = [aValue copy];
} else {
theCopy = aValue;
}

[returnDict setValue:theCopy forKey:key];
}
[result setValue:theCopy forKey:key];
}

return [returnDict copy];
if (mutable) {
return result;
} else {
return [result copy];
}
}

- (NSDictionary *)serializableDeepCopy {
return [self serializableDeepCopy:NO];
}

- (NSMutableDictionary *)serializableMutableDeepCopy {
return [self serializableDeepCopy:YES];
}

@end


@implementation NSArray(SerializableDeepCopy)

-(NSArray *)serializableDeepCopy
-(id)serializableDeepCopy:(BOOL)mutable
{
NSMutableArray *returnArray = [[NSMutableArray alloc] initWithCapacity:self.count];
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:self.count];

for (id aValue in self) {
id theCopy = nil;
Expand All @@ -299,16 +311,30 @@ -(NSArray *)serializableDeepCopy
}

if ([aValue conformsToProtocol:@protocol(SEGSerializableDeepCopy)]) {
theCopy = [aValue serializableDeepCopy];
theCopy = [aValue serializableDeepCopy:mutable];
} else if ([aValue conformsToProtocol:@protocol(NSCopying)]) {
theCopy = [aValue copy];
} else {
theCopy = aValue;
}
[returnArray addObject:theCopy];
[result addObject:theCopy];
}

return [returnArray copy];
if (mutable) {
return result;
} else {
return [result copy];
}
}


- (NSArray *)serializableDeepCopy {
return [self serializableDeepCopy:NO];
}

- (NSMutableArray *)serializableMutableDeepCopy {
return [self serializableDeepCopy:YES];
}


@end
7 changes: 7 additions & 0 deletions Analytics/Classes/SEGAnalyticsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ typedef NSMutableURLRequest *_Nonnull (^SEGRequestFactory)(NSURL *_Nonnull);
*/
@property (nonatomic, strong, nullable) id<SEGCrypto> crypto;


/**
* Set the default settings to use if Segment.com cannot be reached. JSON->NSDictionary conversion is handled for you.
* An example configuration can be found here, using your write key: https://cdn-settings.segment.com/v1/projects/YOUR_WRITE_KEY/settings
*/
@property (nonatomic, strong, nullable) NSDictionary *defaultSettings;

/**
* Set custom middlewares. Will be run before all integrations.
* This property is deprecated in favor of the `sourceMiddleware` property.
Expand Down