@@ -82,35 +82,40 @@ - (BOOL)findTransactionId:(NSString *)transactionId {
8282#pragma mark - Private & helper methods
8383
8484- (void )assignUuid : (NSString *)uuid {
85- // First check if there's any UUID written in keychain.
86- // If yes, use keychain value and flag it.
87- // If not, use given UUID and store it to keychain.
88- // If successfully written, flag it.
89- // If writing failed, don't flat it.
90- // Small addition as of iOS 10.3:
91- // Keychain needs to be written in different way in order to persist value between (un)installs.
92-
93- // Check if device supports new writing method.
94- // kSecAttrAccessGroupToken available since iOS 10.0.
95- if (NULL == &kSecAttrAccessGroupToken ) {
96- // This device's iOS version doesn't support new writing method.
97- [self assignUuidOldMethod: uuid];
98- return ;
99- } else {
100- // This device's iOS version supports new writing method.
101- [self assignUuidNewMethod: uuid];
102- return ;
85+ // 1. Check if UUID is written to keychain in v2 way.
86+ // 1.1 If yes, take stored UUID and send it to v1 check.
87+ // 1.2 If not, take given UUID and send it to v1 check.
88+ // v1 check:
89+ // 2.1 If given UUID is found in v1 way, use it.
90+ // 2.2 If given UUID is not found in v1 way, write it in v1 way and use it.
91+
92+ // First check if we have the key written with app's unique key name.
93+ NSString *uniqueKey = [self generateUniqueKey ];
94+ NSString *persistedUuidUnique = [ADJKeychain valueForKeychainKeyV2: uniqueKey service: @" deviceInfo" ];
95+
96+ if (persistedUuidUnique != nil ) {
97+ // Check if value has UUID format.
98+ if ((bool )[[NSUUID alloc ] initWithUUIDString: persistedUuidUnique]) {
99+ [[ADJAdjustFactory logger ] verbose: @" Value found and read from the keychain v2 way" ];
100+
101+ // If we read the key with v2 way, write it back in v1 way since in iOS 11, that's the only one that it works.
102+ [self assignUuidOldMethod: persistedUuidUnique];
103+ }
103104 }
105+
106+ // At this point, UUID was not persisted in v2 way or if persisted, didn't have proper UUID format.
107+ // Try the v1 way with given UUID.
108+ [self assignUuidOldMethod: uuid];
104109}
105110
106111- (void )assignUuidOldMethod : (NSString *)uuid {
107- NSString *persistedUuid = [ADJKeychain valueForKeychainKeyOld :@" adjust_persisted_uuid" service: @" deviceInfo" ];
112+ NSString *persistedUuid = [ADJKeychain valueForKeychainKeyV1 :@" adjust_persisted_uuid" service: @" deviceInfo" ];
108113
109114 // Check if value exists in keychain.
110115 if (persistedUuid != nil ) {
111116 // Check if value has UUID format.
112117 if ((bool )[[NSUUID alloc ] initWithUUIDString: persistedUuid]) {
113- [[ADJAdjustFactory logger ] verbose: @" Value found and read from the keychain old way" ];
118+ [[ADJAdjustFactory logger ] verbose: @" Value found and read from the keychain v1 way" ];
114119
115120 // Value written in keychain seems to have UUID format.
116121 self.uuid = persistedUuid;
@@ -120,10 +125,10 @@ - (void)assignUuidOldMethod:(NSString *)uuid {
120125 }
121126 }
122127
123- // At this point, UUID was not persisted or if persisted, didn't have proper UUID format.
128+ // At this point, UUID was not persisted in v1 way or if persisted, didn't have proper UUID format.
124129
125130 // Since we don't have anything in the keychain, we'll use the passed UUID value.
126- // Try to save that value to the keychain and flag if successfully written.
131+ // Try to save that value to the keychain in v1 way and flag if successfully written.
127132 self.uuid = uuid;
128133 self.isPersisted = [ADJKeychain setValue: self .uuid forKeychainKey: @" adjust_persisted_uuid" inService: @" deviceInfo" ];
129134}
@@ -144,49 +149,6 @@ - (NSString *)generateUniqueKey {
144149 return [joinedKey adjSha1 ];
145150}
146151
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 ) {
153- // Check if value has UUID format.
154- if ((bool )[[NSUUID alloc ] initWithUUIDString: persistedUuidUnique]) {
155- [[ADJAdjustFactory logger ] verbose: @" Value found and read from the keychain new way" ];
156-
157- // Value written in keychain seems to have UUID format.
158- self.uuid = persistedUuidUnique;
159- self.isPersisted = YES ;
160-
161- return ;
162- }
163- }
164-
165- // At this point, UUID was not persisted with unique key or if persisted, didn't have proper UUID format.
166-
167- // Check if it's still saved in the keychain with old writing method.
168- NSString *persistedUuidOld = [ADJKeychain valueForKeychainKeyOld: @" adjust_persisted_uuid" service: @" deviceInfo" ];
169-
170- if (persistedUuidOld != nil ) {
171- // Check if value has UUID format.
172- if ((bool )[[NSUUID alloc ] initWithUUIDString: persistedUuidOld]) {
173- [[ADJAdjustFactory logger ] verbose: @" Value found and read from the keychain old way" ];
174-
175- // Since we have the value in the keychain written with old method, we'll use it to save it with new one.
176- self.uuid = persistedUuidOld;
177- } else {
178- // Since found value in the keychain doesn't have propper UUID format, we'll use passed UUID value.
179- self.uuid = uuid;
180- }
181- } else {
182- // Since we didn't find anything in the keychain with old method as well, we'll use the passed UUID value.
183- self.uuid = uuid;
184- }
185-
186- // Try to save that value to the keychain and flag if successfully written.
187- self.isPersisted = [ADJKeychain setValue: self .uuid forKeychainKey: uniqueKey inService: @" deviceInfo" ];
188- }
189-
190152- (NSString *)description {
191153 return [NSString stringWithFormat: @" ec:%d sc:%d ssc:%d ask:%d sl:%.1f ts:%.1f la:%.1f dt:%@ " ,
192154 self .eventCount, self .sessionCount, self .subsessionCount, self .askingAttribution, self .sessionLength,
0 commit comments