Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Sources/Amplitude/AMPIdentify.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,19 @@ - (void)addToUserProperties:(NSString *)operation property:(NSString *)property

// Check if property was already used in a previous operation.
if ([_userProperties containsObject:property]) {
AMPLITUDE_LOG(@"Already used property '%@' in previous operation, ignoring for operation '%@'", property, operation);
return;
// Overwrite previous $set value if present
BOOL shouldOverwriteProperty = NO;
if ([operation isEqualToString:AMP_OP_SET]) {
NSMutableDictionary *setOperations = [_userPropertyOperations objectForKey:AMP_OP_SET];
if (setOperations[property] != nil) {
shouldOverwriteProperty = YES;
}
}

if (shouldOverwriteProperty == NO) {
AMPLITUDE_LOG(@"Already used property '%@' in previous operation, ignoring for operation '%@'", property, operation);
return;
}
}

NSMutableDictionary *operations = [_userPropertyOperations objectForKey:operation];
Expand Down
1 change: 1 addition & 0 deletions Sources/Amplitude/AMPURLSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@interface AMPURLSession : ISPPinnedNSURLSessionDelegate <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>

+ (AMPURLSession *)sharedSession;
- (instancetype)initWithConfiguration:(NSURLSessionConfiguration *)configuration;
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;

@end
Expand Down
5 changes: 4 additions & 1 deletion Sources/Amplitude/AMPURLSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ + (AMPURLSession *)sharedSession {
}

- (instancetype)init {
return [self initWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}

- (instancetype)initWithConfiguration:(NSURLSessionConfiguration *)configuration {
if ((self = [super init])) {
[AMPURLSession pinSSLCertificate:@[@"ComodoRsaDomainValidationCA"]];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
_sharedSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
}
return self;
Expand Down
38 changes: 33 additions & 5 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ @interface Amplitude ()

@property (nonatomic, strong) NSOperationQueue *backgroundQueue;
@property (nonatomic, strong) NSOperationQueue *initializerQueue;
@property (nonatomic, strong) id urlSession;
@property (nonatomic, strong) AMPDatabaseHelper *dbHelper;
@property (nonatomic, assign) BOOL initialized;
@property (nonatomic, assign) BOOL sslPinningEnabled;
Expand Down Expand Up @@ -137,6 +138,7 @@ @implementation Amplitude {

BOOL _inForeground;
BOOL _offline;
BOOL _sendOverInexpensiveNetworkOnly;

NSString *_serverUrl;
NSString *_token;
Expand Down Expand Up @@ -204,6 +206,7 @@ - (instancetype)initWithInstanceName:(NSString *)instanceName {
_useAdvertisingIdForDeviceId = NO;
_backoffUpload = NO;
_offline = NO;
_sendOverInexpensiveNetworkOnly = NO;
_serverUrl = kAMPEventLogUrl;
_serverZone = US;
self.libraryName = kAMPLibrary;
Expand Down Expand Up @@ -991,13 +994,30 @@ - (void)makeEventUploadPostRequest:(NSString *)url events:(NSString *)events num
[request setHTTPBody:postData];
AMPLITUDE_LOG(@"Events: %@", events);

// If pinning is enabled, use the AMPURLSession that handles it.
// Configure the appropriate URL session
if (self.urlSession == nil) {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
if (self->_sendOverInexpensiveNetworkOnly) {
configuration.allowsCellularAccess = NO;
if (@available(iOS 13.0, *)) {
configuration.allowsExpensiveNetworkAccess = NO;
configuration.allowsConstrainedNetworkAccess = NO;
}
}

if (self.sslPinningEnabled) {
// If pinning is enabled, use the AMPURLSession that handles it.
#if AMPLITUDE_SSL_PINNING
id session = (self.sslPinningEnabled ? [AMPURLSession class] : [NSURLSession class]);
#else
id session = [NSURLSession class];
self.urlSession = [[AMPURLSession alloc] initWithConfiguration:configuration];
#endif
[[[session sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
} else {
self.urlSession = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
}
}

id session = self.urlSession;

[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
BOOL uploadSuccessful = NO;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (response != nil) {
Expand Down Expand Up @@ -1425,6 +1445,14 @@ - (void)setOffline:(BOOL)offline {
}
}

- (void)setSendOverInexpensiveNetworkOnly:(BOOL)sendOverInexpensiveNetworkOnly {
if (_sendOverInexpensiveNetworkOnly != sendOverInexpensiveNetworkOnly) {
_sendOverInexpensiveNetworkOnly = sendOverInexpensiveNetworkOnly;
self.urlSession = nil;
}
}


- (void)setServerUrl:(NSString *)serverUrl {
if (!(serverUrl == nil || [self isArgument:serverUrl validType:[NSString class] methodName:@"setServerUrl:"])) {
return;
Expand Down
9 changes: 9 additions & 0 deletions Sources/Amplitude/Public/Amplitude.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ typedef void (^AMPInitCompletionBlock)(void);
*/
- (void)setOffline:(BOOL)offline;

/**
Use only non-cellular, inexpensive and non-constrained network for sending of logged events to Amplitude servers.

Setting this to YES would prevent the use of expensive network for sending logged events to Amplitude servers. Expensive network includes cellular network, and in iOS 13 and later—personal hotspot and any network explicitly marked by user for low data use.

@param sendOverInexpensiveNetworkOnly Whether logged events should be sent over inexpensive network only
*/
- (void)setSendOverInexpensiveNetworkOnly:(BOOL)sendOverInexpensiveNetworkOnly;

/**
Uses advertisingIdentifier instead of identifierForVendor as the device ID

Expand Down