Skip to content

Commit 14b308e

Browse files
committed
Merge pull request #40 from adjust/disable_SDK
V3.2.0 Disable SDK
2 parents 79c13a1 + eaa2175 commit 14b308e

14 files changed

+198
-20
lines changed

Adjust/AIActivityHandler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
withParameters:(NSDictionary *)parameters;
3131

3232
- (void)finishedTrackingWithResponse:(AIResponseData *)response;
33+
- (void)setEnabled:(BOOL)enabled;
34+
- (BOOL)isEnabled;
3335

3436
@end
3537

Adjust/AIActivityHandler.m

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ @interface AIActivityHandler()
4242
@property (nonatomic, copy) NSString *userAgent;
4343
@property (nonatomic, copy) NSString *clientSdk;
4444
@property (nonatomic, assign) BOOL trackingEnabled;
45+
@property (nonatomic, assign) BOOL internalEnabled;
4546

4647
@end
4748

@@ -70,6 +71,7 @@ - (id)initWithAppToken:(NSString *)yourAppToken {
7071
// default values
7172
self.environment = @"unknown";
7273
self.trackMacMd5 = YES;
74+
self.internalEnabled = YES;
7375

7476
dispatch_async(self.internalQueue, ^{
7577
[self initInternal:yourAppToken];
@@ -119,6 +121,26 @@ - (void)finishedTrackingWithResponse:(AIResponseData *)response {
119121
}
120122
}
121123

124+
- (void)setEnabled:(BOOL)enabled {
125+
self.internalEnabled = enabled;
126+
if ([self checkActivityState:self.activityState]) {
127+
self.activityState.enabled = enabled;
128+
}
129+
if (enabled) {
130+
[self trackSubsessionStart];
131+
} else {
132+
[self trackSubsessionEnd];
133+
}
134+
}
135+
136+
- (BOOL)isEnabled {
137+
if ([self checkActivityState:self.activityState]) {
138+
return self.activityState.enabled;
139+
} else {
140+
return self.internalEnabled;
141+
}
142+
}
143+
122144
#pragma mark - internal
123145
- (void)initInternal:(NSString *)yourAppToken {
124146
if (![self checkAppTokenNotNil:yourAppToken]) return;
@@ -144,6 +166,11 @@ - (void)initInternal:(NSString *)yourAppToken {
144166
- (void)startInternal {
145167
if (![self checkAppTokenNotNil:self.appToken]) return;
146168

169+
if (self.activityState != nil
170+
&& !self.activityState.enabled) {
171+
return;
172+
}
173+
147174
[self.packageHandler resumeSending];
148175
[self startTimer];
149176

@@ -157,6 +184,7 @@ - (void)startInternal {
157184

158185
[self transferSessionPackage];
159186
[self.activityState resetSessionAttributes:now];
187+
self.activityState.enabled = self.internalEnabled;
160188
[self writeActivityState];
161189
[self.logger info:@"First session"];
162190
return;
@@ -200,7 +228,8 @@ - (void)endInternal {
200228

201229
[self.packageHandler pauseSending];
202230
[self stopTimer];
203-
[self updateActivityState];
231+
double now = [NSDate.date timeIntervalSince1970];
232+
[self updateActivityState:now];
204233
[self writeActivityState];
205234
}
206235

@@ -212,12 +241,16 @@ - (void)eventInternal:(NSString *)eventToken
212241
if (![self checkEventTokenNotNil:eventToken]) return;
213242
if (![self checkEventTokenLength:eventToken]) return;
214243

244+
if (!self.activityState.enabled) {
245+
return;
246+
}
247+
215248
AIPackageBuilder *eventBuilder = [[AIPackageBuilder alloc] init];
216249
eventBuilder.eventToken = eventToken;
217250
eventBuilder.callbackParameters = parameters;
218251

219252
double now = [NSDate.date timeIntervalSince1970];
220-
[self updateActivityState];
253+
[self updateActivityState:now];
221254
self.activityState.createdAt = now;
222255
self.activityState.eventCount++;
223256

@@ -247,13 +280,17 @@ - (void)revenueInternal:(double)amount
247280
if (![self checkEventTokenLength:eventToken]) return;
248281
if (![self checkTransactionId:transactionId]) return;
249282

283+
if (!self.activityState.enabled) {
284+
return;
285+
}
286+
250287
AIPackageBuilder *revenueBuilder = [[AIPackageBuilder alloc] init];
251288
revenueBuilder.amountInCents = amount;
252289
revenueBuilder.eventToken = eventToken;
253290
revenueBuilder.callbackParameters = parameters;
254291

255292
double now = [NSDate.date timeIntervalSince1970];
256-
[self updateActivityState];
293+
[self updateActivityState:now];
257294
self.activityState.createdAt = now;
258295
self.activityState.eventCount++;
259296

@@ -275,10 +312,9 @@ - (void)revenueInternal:(double)amount
275312
#pragma mark - private
276313

277314
// returns whether or not the activity state should be written
278-
- (BOOL)updateActivityState {
315+
- (BOOL)updateActivityState:(double)now {
279316
if (![self checkActivityState:self.activityState]) return NO;
280317

281-
double now = [NSDate.date timeIntervalSince1970];
282318
double lastInterval = now - self.activityState.lastActivity;
283319
if (lastInterval < 0) {
284320
[self.logger error:@"Time travel!"];
@@ -375,8 +411,13 @@ - (void)stopTimer {
375411
}
376412

377413
- (void)timerFired {
414+
if (self.activityState != nil
415+
&& !self.activityState.enabled) {
416+
return;
417+
}
378418
[self.packageHandler sendFirstPackage];
379-
if ([self updateActivityState]) {
419+
double now = [NSDate.date timeIntervalSince1970];
420+
if ([self updateActivityState:now]) {
380421
[self writeActivityState];
381422
}
382423
}

Adjust/AIActivityState.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
// persistent data
1515
@property (nonatomic, copy) NSString *uuid;
16+
@property (nonatomic, assign) BOOL enabled;
1617

1718
// global counters
1819
@property (nonatomic, assign) int eventCount;

Adjust/AIActivityState.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ - (id)init {
3232
self.createdAt = -1;
3333
self.lastInterval = -1;
3434
self.transactionIds = [NSMutableArray arrayWithCapacity:kTransactionIdCount];
35+
self.enabled = YES;
3536

3637
return self;
3738
}
@@ -92,6 +93,7 @@ - (id)initWithCoder:(NSCoder *)decoder {
9293
self.lastActivity = [decoder decodeDoubleForKey:@"lastActivity"];
9394
self.uuid = [decoder decodeObjectForKey:@"uuid"];
9495
self.transactionIds = [decoder decodeObjectForKey:@"transactionIds"];
96+
self.enabled = [decoder decodeBoolForKey:@"enabled"];
9597

9698
// create UUID for migrating devices
9799
if (self.uuid == nil) {
@@ -102,6 +104,10 @@ - (id)initWithCoder:(NSCoder *)decoder {
102104
self.transactionIds = [NSMutableArray arrayWithCapacity:kTransactionIdCount];
103105
}
104106

107+
if (![decoder containsValueForKey:@"enabled"]) {
108+
self.enabled = YES;
109+
}
110+
105111
self.lastInterval = -1;
106112

107113
return self;
@@ -117,6 +123,7 @@ - (void)encodeWithCoder:(NSCoder *)encoder {
117123
[encoder encodeDouble:self.lastActivity forKey:@"lastActivity"];
118124
[encoder encodeObject:self.uuid forKey:@"uuid"];
119125
[encoder encodeObject:self.transactionIds forKey:@"transactionIds"];
126+
[encoder encodeBool:self.enabled forKey:@"enabled"];
120127
}
121128

122129

Adjust/AIPackageBuilder.m

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
#import "AIPackageBuilder.h"
1010
#import "AIActivityPackage.h"
1111
#import "NSData+AIAdditions.h"
12-
13-
static NSString * const kDateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'Z";
14-
static NSDateFormatter * dateFormat;
12+
#import "AIUtil.h"
1513

1614
#pragma mark -
1715
@implementation AIPackageBuilder
@@ -129,8 +127,7 @@ - (void)parameters:(NSMutableDictionary *)parameters setInt:(int)value forKey:(N
129127
- (void)parameters:(NSMutableDictionary *)parameters setDate:(double)value forKey:(NSString *)key {
130128
if (value < 0) return;
131129

132-
NSDate *date = [NSDate dateWithTimeIntervalSince1970:value];
133-
NSString *dateString = [self.dateFormat stringFromDate:date];
130+
NSString *dateString = [AIUtil dateFormat:value];
134131
[self parameters:parameters setString:dateString forKey:key];
135132
}
136133

@@ -149,13 +146,5 @@ - (void)parameters:(NSMutableDictionary *)parameters setDictionary:(NSDictionary
149146
[self parameters:parameters setString:dictionaryString forKey:key];
150147
}
151148

152-
- (NSDateFormatter *)dateFormat {
153-
if (dateFormat == nil) {
154-
dateFormat = [[NSDateFormatter alloc] init];
155-
[dateFormat setDateFormat:kDateFormat];
156-
}
157-
return dateFormat;
158-
}
159-
160149
@end
161150

Adjust/AIRequestHandler.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ - (NSData *)bodyForParameters:(NSDictionary *)parameters {
117117
[pairs addObject:pair];
118118
}
119119

120+
double now = [NSDate.date timeIntervalSince1970];
121+
NSString *dateString = [AIUtil dateFormat:now];
122+
NSString *escapedDate = [dateString aiUrlEncode];
123+
NSString *sentAtPair = [NSString stringWithFormat:@"%@=%@", @"sent_at", escapedDate];
124+
[pairs addObject:sentAtPair];
125+
120126
NSString *bodyString = [pairs componentsJoinedByString:@"&"];
121127
NSData *body = [NSData dataWithBytes:bodyString.UTF8String length:bodyString.length];
122128
return body;

Adjust/AIUtil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
+ (NSString *)userAgent;
1515

1616
+ (void)excludeFromBackup:(NSString *)filename;
17+
+ (NSString *)dateFormat:(double)value;
1718

1819
@end

Adjust/AIUtil.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
static NSString * const kBaseUrl = @"https://app.adjust.io";
1717
static NSString * const kClientSdk = @"ios3.1.0";
1818

19+
static NSString * const kDateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'Z";
20+
static NSDateFormatter * dateFormat;
21+
1922

2023
#pragma mark -
2124
@implementation AIUtil
@@ -110,4 +113,15 @@ + (void)excludeFromBackup:(NSString *)path {
110113
}
111114
}
112115

116+
+ (NSString *)dateFormat:(double) value {
117+
if (dateFormat == nil) {
118+
dateFormat = [[NSDateFormatter alloc] init];
119+
[dateFormat setDateFormat:kDateFormat];
120+
}
121+
122+
NSDate *date = [NSDate dateWithTimeIntervalSince1970:value];
123+
124+
return [dateFormat stringFromDate:date];
125+
}
126+
113127
@end

Adjust/Adjust.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ static NSString * const AIEnvironmentProduction = @"production";
152152
*/
153153
+ (void)trackSubsessionEnd;
154154

155+
/**
156+
* Enable or disable the adjust SDK
157+
*
158+
* @param enabled The flag to enable or disable the adjust SDK
159+
*/
160+
+ (void)setEnabled:(BOOL)enabled;
161+
162+
/**
163+
* Check if the SDK is enabled or disabled
164+
*/
165+
+ (BOOL)isEnabled;
166+
155167
@end
156168

157169

Adjust/Adjust.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,12 @@ + (void)trackSubsessionEnd {
124124
[activityHandler trackSubsessionEnd];
125125
}
126126

127+
+ (void)setEnabled:(BOOL)enabled {
128+
[activityHandler setEnabled:enabled];
129+
}
130+
131+
+ (BOOL)isEnabled {
132+
return [activityHandler isEnabled];
133+
}
134+
127135
@end

0 commit comments

Comments
 (0)