1010#import " ADJAdjustFactory.h"
1111#import " ADJActivityState.h"
1212#import " UIDevice+ADJAdditions.h"
13+ #import " NSString+ADJAdditions.h"
1314
1415static const int kTransactionIdCount = 10 ;
16+ static NSString *appToken = nil ;
1517
1618@implementation ADJActivityState
1719
@@ -44,6 +46,12 @@ - (id)init {
4446
4547#pragma mark - Public methods
4648
49+ + (void )saveAppToken : (NSString *)appTokenToSave {
50+ @synchronized (self) {
51+ appToken = appTokenToSave;
52+ }
53+ }
54+
4755- (void )resetSessionAttributes : (double )now {
4856 self.subsessionCount = 1 ;
4957 self.sessionLength = 0 ;
@@ -120,31 +128,50 @@ - (void)assignUuidOldMethod:(NSString *)uuid {
120128 self.isPersisted = [ADJKeychain setValue: self .uuid forKeychainKey: @" adjust_persisted_uuid" inService: @" deviceInfo" ];
121129}
122130
123- - (void )assignUuidNewMethod : (NSString *)uuid {
124- NSString *persistedUuidNew = [ADJKeychain valueForKeychainKeyNew: @" adjust_persisted_uuid" service: @" deviceInfo" ];
131+ - (NSString *)generateUniqueKey {
132+ if (appToken == nil ) {
133+ return nil ;
134+ }
125135
126- // Check if value exists in keychain.
127- if (persistedUuidNew != nil ) {
136+ NSString *bundleIdentifier = [[NSBundle mainBundle ] bundleIdentifier ];
137+
138+ if (bundleIdentifier == nil ) {
139+ return nil ;
140+ }
141+
142+ NSString *joinedKey = [NSString stringWithFormat: @" %@%@ " , bundleIdentifier, appToken];
143+
144+ return [joinedKey adjSha1 ];
145+ }
146+
147+ - (void )assignUuidNewMethod : (NSString *)uuid {
148+ // First check if we have the key written with app's unique key name.
149+ NSString *uniqueKey = [self generateUniqueKey ];
150+ NSString *persistedUuidUnique = [ADJKeychain valueForKeychainKeyNew: uniqueKey service: @" deviceInfo" ];
151+
152+ if (persistedUuidUnique != nil ) {
128153 // Check if value has UUID format.
129- if ((bool )[[NSUUID alloc ] initWithUUIDString: persistedUuidNew ]) {
154+ if ((bool )[[NSUUID alloc ] initWithUUIDString: persistedUuidUnique ]) {
130155 [[ADJAdjustFactory logger ] verbose: @" Value found and read from the keychain new way" ];
131-
156+
132157 // Value written in keychain seems to have UUID format.
133- self.uuid = persistedUuidNew ;
158+ self.uuid = persistedUuidUnique ;
134159 self.isPersisted = YES ;
135-
160+
136161 return ;
137162 }
138163 }
139164
140- // At this point, UUID was not persisted with new method or if persisted, didn't have proper UUID format.
165+ // At this point, UUID was not persisted with unique key or if persisted, didn't have proper UUID format.
141166
142167 // Check if it's still saved in the keychain with old writing method.
143168 NSString *persistedUuidOld = [ADJKeychain valueForKeychainKeyOld: @" adjust_persisted_uuid" service: @" deviceInfo" ];
144169
145170 if (persistedUuidOld != nil ) {
146171 // Check if value has UUID format.
147172 if ((bool )[[NSUUID alloc ] initWithUUIDString: persistedUuidOld]) {
173+ [[ADJAdjustFactory logger ] verbose: @" Value found and read from the keychain old way" ];
174+
148175 // Since we have the value in the keychain written with old method, we'll use it to save it with new one.
149176 self.uuid = persistedUuidOld;
150177 } else {
@@ -157,7 +184,7 @@ - (void)assignUuidNewMethod:(NSString *)uuid {
157184 }
158185
159186 // Try to save that value to the keychain and flag if successfully written.
160- self.isPersisted = [ADJKeychain setValue: self .uuid forKeychainKey: @" adjust_persisted_uuid " inService: @" deviceInfo" ];
187+ self.isPersisted = [ADJKeychain setValue: self .uuid forKeychainKey: uniqueKey inService: @" deviceInfo" ];
161188}
162189
163190- (NSString *)description {
0 commit comments