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

SDK-3857-UserDefaults_Migration #341

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CleverTapSDK/CTPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@

+ (NSString *_Nonnull)filePathfromFileName:(NSString *_Nonnull)filename;

+ (void) migrateCTUserDefaultsData;

@end
39 changes: 32 additions & 7 deletions CleverTapSDK/CTPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#import "CTConstants.h"

#define PREF_PREFIX @"WizRocket"
#define SUITE_NAME @"CleverTap"

@implementation CTPreferences

+ (long)getIntForKey:(NSString *)key withResetValue:(long)resetValue {
key = [NSString stringWithFormat:@"%@%@", PREF_PREFIX, key];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME];
id value = [defaults objectForKey:key];
if ([value isKindOfClass:[NSNumber class]]) {
return ((long) [value longLongValue]);
Expand All @@ -20,14 +22,16 @@ + (long)getIntForKey:(NSString *)key withResetValue:(long)resetValue {

+ (void)putInt:(long)resetValue forKey:(NSString *)key {
key = [NSString stringWithFormat:@"%@%@", PREF_PREFIX, key];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME];
[defaults setObject:@(resetValue) forKey:key];
[defaults synchronize];
}

+ (NSString *_Nullable)getStringForKey:(NSString *_Nonnull)key withResetValue:(NSString *_Nullable)resetValue {
key = [NSString stringWithFormat:@"%@%@", PREF_PREFIX, key];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME];
id value = [defaults objectForKey:key];
if ([value isKindOfClass:[NSString class]]) {
return value;
Expand All @@ -42,27 +46,31 @@ + (NSString *_Nullable)getStringForKey:(NSString *_Nonnull)key withResetValue:(N

+ (void)putString:(NSString *)resetValue forKey:(NSString *)key {
key = [NSString stringWithFormat:@"%@%@", PREF_PREFIX, key];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME];
[defaults setObject:resetValue forKey:key];
[defaults synchronize];
}

+ (id)getObjectForKey:(NSString *)key {
key = [NSString stringWithFormat:@"%@%@", PREF_PREFIX, key];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME];
return [defaults objectForKey:key];
}

+ (void)putObject:(id)object forKey:(NSString *)key {
key = [NSString stringWithFormat:@"%@%@", PREF_PREFIX, key];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME];
[defaults setObject:object forKey:key];
[defaults synchronize];
}

+ (void)removeObjectForKey:(NSString *)key {
key = [NSString stringWithFormat:@"%@%@", PREF_PREFIX, key];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME];
[defaults removeObjectForKey:key];
[defaults synchronize];
}
Expand Down Expand Up @@ -191,4 +199,21 @@ + (NSString * _Nonnull)storageKeyWithSuffix: (NSString * _Nonnull)suffix config:
return [NSString stringWithFormat:@"%@:%@", config.accountId, suffix];
}

+ (void)migrateCTUserDefaultsData{
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *standardDefaultsDict = [standardDefaults dictionaryRepresentation];
NSUserDefaults *ctDefaults = [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME];

for (NSString *key in standardDefaultsDict) {
if ([key containsString:PREF_PREFIX]){
id value = standardDefaultsDict[key];
[ctDefaults setObject:value forKey:key];
[standardDefaults removeObjectForKey:key];
}
}

[standardDefaults synchronize];
[ctDefaults synchronize];
}

@end
1 change: 1 addition & 0 deletions CleverTapSDK/CleverTap.m
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ + (instancetype)_instanceWithConfig:(CleverTapInstanceConfig *)config andCleverT
- (instancetype)initWithConfig:(CleverTapInstanceConfig*)config andCleverTapID:(NSString *)cleverTapID {
self = [super init];
if (self) {
[CTPreferences migrateCTUserDefaultsData];
_config = [config copy];
if (_config.analyticsOnly) {
CleverTapLogDebug(_config.logLevel, @"%@ is configured as analytics only!", self);
Expand Down
49 changes: 39 additions & 10 deletions CleverTapSDKTests/CTPreferencesTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ @interface CTPreferencesTest : XCTestCase
@implementation CTPreferencesTest

- (void)setUp {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"CleverTap"];
[defaults setObject:@(2333333333333333333) forKey:@"WizRocketlongValueForTesting"];
}

- (void)tearDown {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"CleverTap"];
[defaults removeObjectForKey:@"WizRocketlongValueForTesting"];
}

Expand Down Expand Up @@ -51,7 +51,7 @@ -(void)test_putInt_withInvalidKey {
}

- (void)test_getStringForKey_withValidKey {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"CleverTap"];
[defaults setObject:@"stringValueForTesting" forKey:@"WizRocketstringValueForTesting"];

NSString *stringValue = [CTPreferences getStringForKey:@"stringValueForTesting" withResetValue:@"testResetStringValue"];
Expand All @@ -60,7 +60,7 @@ - (void)test_getStringForKey_withValidKey {
}

- (void)test_getStringForKey_withInvalidKey {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"CleverTap"];
[defaults setObject:@"stringValueForTesting" forKey:@"WizRocketstringValueForTesting"];

NSString *stringValue = [CTPreferences getStringForKey:@"invalidTestKey" withResetValue:@"testResetStringValue"];
Expand All @@ -83,18 +83,18 @@ -(void)test_putString_withInvalidKey {
}

- (void)test_getObjectForKey_withValidKey {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"CleverTap"];
[defaults setObject:@"stringValueForTesting" forKey:@"WizRocketstringValueForTesting"];

id idValue = [CTPreferences getObjectForKey:@"stringValueForTesting"];

XCTAssertEqualObjects(idValue, @"stringValueForTesting");
}

- (void)test_getObjectForKey_withInvalidKey {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"CleverTap"];
[defaults setObject:@"stringValueForTesting" forKey:@"WizRocketstringValueForTesting"];

id idValue = [CTPreferences getObjectForKey:@"invalidTestStringKey"];

XCTAssertNil(idValue);
Expand All @@ -112,13 +112,42 @@ -(void)test_putObject_withInvalidKey {
}

-(void)test_removeObjectForKey {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"CleverTap"];
[defaults setObject:@"objectValueForTesting" forKey:@"WizRocketobjectValueForTesting"];

[CTPreferences removeObjectForKey:@"objectValueForTesting"];

id checkValue = [CTPreferences getObjectForKey:@"WizRocketobjectValueForTesting"];

XCTAssertNil(checkValue);
}

-(void)test_migrateCTUserDefaultsData {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *ctDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"CleverTap"];

// Set test data
[defaults setObject:@"TestValue1" forKey:@"WizRocket_TestKey1"];
[defaults setObject:@"TestValue2" forKey:@"WizRocket_TestKey2"];
[defaults setObject:@"OtherValue" forKey:@"OtherKey"];
[defaults synchronize];

// Call the method to migrate data
[CTPreferences migrateCTUserDefaultsData];

// Verify that data with PREF_PREFIX is migrated to CleverTap user defaults
XCTAssertEqualObjects([ctDefaults objectForKey:@"WizRocket_TestKey1"], @"TestValue1");
XCTAssertEqualObjects([ctDefaults objectForKey:@"WizRocket_TestKey2"], @"TestValue2");

// Verify that data without PREF_PREFIX is not migrated
XCTAssertNil([ctDefaults objectForKey:@"OtherKey"]);

// Verify that data with PREF_PREFIX is removed from standard user defaults
XCTAssertNil([defaults objectForKey:@"WizRocket_TestKey1"]);
XCTAssertNil([defaults objectForKey:@"WizRocket_TestKey2"]);

// Verify that data without PREF_PREFIX is not removed from standard user defaults
XCTAssertEqualObjects([defaults objectForKey:@"OtherKey"], @"OtherValue");
}

@end
Loading