Skip to content

Commit 74d3225

Browse files
authored
Switch config info to LPAPIConfig (#197)
1 parent b6b5d70 commit 74d3225

File tree

10 files changed

+141
-113
lines changed

10 files changed

+141
-113
lines changed

Example/Tests/Classes/Utilities/LeanplumHelper.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#import "LeanplumReachability+Category.h"
3636
#import "LPNetworkEngine+Category.h"
3737
#import "LPNetworkOperation+Category.h"
38+
#import "LPAPIConfig.h"
3839

3940
NSString *APPLICATION_ID = @"app_nLiaLr3lXvCjXhsztS1Gw8j281cPLO6sZetTDxYnaSk";
4041
NSString *DEVELOPMENT_KEY = @"dev_2bbeWLmVJyNrqI8F21Kn9nqyUPRkVCUoLddBkHEyzmk";
@@ -123,9 +124,9 @@ + (void)clean_up {
123124
[[LPVarCache sharedCache] reset];
124125
[[LPVarCache sharedCache] initialize];
125126
[LPActionManager reset];
126-
[LeanplumRequest setDeviceId:nil];
127-
[LeanplumRequest setUserId:nil];
128-
[LeanplumRequest setToken:nil];
127+
[[LPAPIConfig sharedConfig] setDeviceId:nil];
128+
[[LPAPIConfig sharedConfig] setUserId:nil];
129+
[[LPAPIConfig sharedConfig] setToken:nil];
129130
[LeanplumRequest reset];
130131
[LeanplumHelper reset_user_defaults];
131132
[[LeanplumRequest sendNowQueue] cancelAllOperations];

Leanplum-SDK/Classes/Features/Actions/LPActionManager.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#import "LeanplumRequest.h"
3535
#import "LPMessageTemplates.h"
3636
#import "LPRequestFactory.h"
37+
#import "LPRequestManager.h"
38+
#import "LPAPIConfig.h"
3739

3840
#import <objc/runtime.h>
3941
#import <objc/message.h>
@@ -106,7 +108,7 @@ - (NSString *)leanplum_createUserNotificationSettingsKey
106108
{
107109
return [NSString stringWithFormat:
108110
LEANPLUM_DEFAULTS_USER_NOTIFICATION_SETTINGS_KEY,
109-
LeanplumRequest.appId, LeanplumRequest.userId, LeanplumRequest.deviceId];
111+
[LPAPIConfig sharedConfig].appId, [LPAPIConfig sharedConfig].userId, [LPAPIConfig sharedConfig].deviceId];
110112
}
111113

112114
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 && LP_NOT_TV

Leanplum-SDK/Classes/Features/Variables/LPVarCache.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#import "Leanplum_SocketIO.h"
3535
#import "Utils.h"
3636
#import "LPRequestFactory.h"
37+
#import "LPRequestManager.h"
38+
#import "LPAPIConfig.h"
3739

3840
@interface LPVarCache()
3941
@property (strong, nonatomic) NSRegularExpression *varNameRegex;
@@ -391,10 +393,10 @@ - (void)loadDiffs
391393
BOOL loggingEnabled = [archiver decodeBoolForKey:LP_KEY_LOGGING_ENABLED];
392394

393395
if (deviceId) {
394-
[LeanplumRequest setDeviceId:deviceId];
396+
[[LPAPIConfig sharedConfig] setDeviceId:deviceId];
395397
}
396398
if (userId) {
397-
[LeanplumRequest setUserId:userId];
399+
[[LPAPIConfig sharedConfig] setUserId:userId];
398400
}
399401
if (loggingEnabled) {
400402
[LPConstantsState sharedState].loggingEnabled = YES;
@@ -432,8 +434,8 @@ - (void)saveDiffs
432434
[archiver encodeObject:self.variantDebugInfo forKey:LP_KEY_VARIANT_DEBUG_INFO];
433435
[archiver encodeObject:self.regions forKey:LP_KEY_REGIONS];
434436
[archiver encodeObject:[LPConstantsState sharedState].sdkVersion forKey:LP_PARAM_SDK_VERSION];
435-
[archiver encodeObject:LeanplumRequest.deviceId forKey:LP_PARAM_DEVICE_ID];
436-
[archiver encodeObject:LeanplumRequest.userId forKey:LP_PARAM_USER_ID];
437+
[archiver encodeObject:[LPAPIConfig sharedConfig].deviceId forKey:LP_PARAM_DEVICE_ID];
438+
[archiver encodeObject:[LPAPIConfig sharedConfig].userId forKey:LP_PARAM_USER_ID];
437439
[archiver encodeBool:[LPConstantsState sharedState].loggingEnabled forKey:LP_KEY_LOGGING_ENABLED];
438440
[archiver finishEncoding];
439441

@@ -748,7 +750,7 @@ - (NSMutableDictionary *)userAttributes
748750
{
749751
if (!_userAttributes) {
750752
@try {
751-
NSString *token = [LeanplumRequest token];
753+
NSString *token = [[LPAPIConfig sharedConfig] token];
752754
if (token) {
753755
NSData *encryptedValue = [[NSUserDefaults standardUserDefaults] dataForKey:LEANPLUM_DEFAULTS_ATTRIBUTES_KEY];
754756
if (encryptedValue) {

Leanplum-SDK/Classes/Internal/Leanplum.m

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#import "LPAppIconManager.h"
4747
#import "LPUIEditorWrapper.h"
4848
#import "LPRequestFactory.h"
49+
#import "LPRequestManager.h"
50+
#import "LPAPIConfig.h"
4951

5052
static NSString *leanplum_deviceId = nil;
5153
static NSString *registrationEmail = nil;
@@ -255,7 +257,8 @@ + (void)setAppId:(NSString *)appId withDevelopmentKey:(NSString *)accessKey
255257

256258
LP_TRY
257259
[LPConstantsState sharedState].isDevelopmentModeEnabled = YES;
258-
[LeanplumRequest setAppId:appId withAccessKey:accessKey];
260+
[[LPAPIConfig sharedConfig] setAppId:appId withAccessKey:accessKey];
261+
[LeanplumRequest initializeStaticVars];
259262
LP_END_TRY
260263
}
261264

@@ -280,7 +283,8 @@ + (void)setAppId:(NSString *)appId withProductionKey:(NSString *)accessKey
280283

281284
LP_TRY
282285
[LPConstantsState sharedState].isDevelopmentModeEnabled = NO;
283-
[LeanplumRequest setAppId:appId withAccessKey:accessKey];
286+
[[LPAPIConfig sharedConfig] setAppId:appId withAccessKey:accessKey];
287+
[LeanplumRequest initializeStaticVars];
284288
LP_END_TRY
285289
}
286290

@@ -374,7 +378,7 @@ + (void)synchronizeDefaults
374378
+ (NSString *)pushTokenKey
375379
{
376380
return [NSString stringWithFormat: LEANPLUM_DEFAULTS_PUSH_TOKEN_KEY,
377-
LeanplumRequest.appId, LeanplumRequest.userId, LeanplumRequest.deviceId];
381+
[LPAPIConfig sharedConfig].appId, [LPAPIConfig sharedConfig].userId, [LPAPIConfig sharedConfig].deviceId];
378382
}
379383

380384
+ (void)start
@@ -669,7 +673,7 @@ + (void)startWithUserId:(NSString *)userId
669673
userAttributes:(NSDictionary *)attributes
670674
responseHandler:(LeanplumStartBlock)startResponse
671675
{
672-
if ([LeanplumRequest appId] == nil) {
676+
if ([LPAPIConfig sharedConfig].appId == nil) {
673677
[self throwError:@"Please provide your app ID using one of the [Leanplum setAppId:] "
674678
@"methods."];
675679
return;
@@ -732,7 +736,7 @@ + (void)startWithUserId:(NSString *)userId
732736
});
733737
state.actionManager = [LPActionManager sharedManager];
734738

735-
[LeanplumRequest loadToken];
739+
[[LPAPIConfig sharedConfig] loadToken];
736740
[[LPVarCache sharedCache] setSilent:YES];
737741
[[LPVarCache sharedCache] loadDiffs];
738742
[[LPVarCache sharedCache] setSilent:NO];
@@ -757,7 +761,7 @@ + (void)startWithUserId:(NSString *)userId
757761
}];
758762

759763
// Set device ID.
760-
NSString *deviceId = [LeanplumRequest deviceId];
764+
NSString *deviceId = [LPAPIConfig sharedConfig].deviceId;
761765
// This is the device ID set when the MAC address is used on iOS 7.
762766
// This is to allow apps who upgrade to the new ID to forget the old one.
763767
if ([deviceId isEqualToString:@"0f607264fc6318a92b9e13c65db7cd3c"]) {
@@ -776,17 +780,17 @@ + (void)startWithUserId:(NSString *)userId
776780
if (!deviceId) {
777781
deviceId = [[UIDevice currentDevice] leanplum_uniqueGlobalDeviceIdentifier];
778782
}
779-
[LeanplumRequest setDeviceId:deviceId];
783+
[[LPAPIConfig sharedConfig] setDeviceId:deviceId];
780784
}
781785

782786
// Set user ID.
783787
if (!userId) {
784-
userId = [LeanplumRequest userId];
788+
userId = [LPAPIConfig sharedConfig].userId;
785789
if (!userId) {
786-
userId = [LeanplumRequest deviceId];
790+
userId = [LPAPIConfig sharedConfig].deviceId;
787791
}
788792
}
789-
[LeanplumRequest setUserId:userId];
793+
[[LPAPIConfig sharedConfig] setUserId:userId];
790794

791795
// Setup parameters.
792796
NSString *versionName = [LPInternalState sharedState].appVersion;
@@ -875,8 +879,8 @@ + (void)startWithUserId:(NSString *)userId
875879
NSDictionary *variantDebugInfo = [self parseVariantDebugInfoFromResponse:response];
876880
[[LPVarCache sharedCache] setVariantDebugInfo:variantDebugInfo];
877881

878-
[LeanplumRequest setToken:token];
879-
[LeanplumRequest saveToken];
882+
[[LPAPIConfig sharedConfig] setToken:token];
883+
[[LPAPIConfig sharedConfig] saveToken];
880884
[[LPVarCache sharedCache] applyVariableDiffs:values
881885
messages:messages
882886
updateRules:updateRules
@@ -930,8 +934,8 @@ + (void)startWithUserId:(NSString *)userId
930934
[[LPVarCache sharedCache] setDevModeValuesFromServer:valuesFromCode
931935
fileAttributes:fileAttributes
932936
actionDefinitions:actionDefinitions];
933-
[[LeanplumSocket sharedSocket] connectToAppId:LeanplumRequest.appId
934-
deviceId:LeanplumRequest.deviceId];
937+
[[LeanplumSocket sharedSocket] connectToAppId:[LPAPIConfig sharedConfig].appId
938+
deviceId:[LPAPIConfig sharedConfig].deviceId];
935939
if ([response[LP_KEY_IS_REGISTERED] boolValue]) {
936940
[Leanplum onHasStartedAndRegisteredAsDeveloper];
937941
}
@@ -2044,7 +2048,7 @@ + (void)setUserIdInternal:(NSString *)userId withAttributes:(NSDictionary *)attr
20442048
[req send];
20452049

20462050
if (userId.length) {
2047-
[LeanplumRequest setUserId:userId];
2051+
[[LPAPIConfig sharedConfig] setUserId:userId];
20482052
if ([LPInternalState sharedState].hasStarted) {
20492053
[[LPVarCache sharedCache] saveDiffs];
20502054
}
@@ -2441,7 +2445,7 @@ + (NSString *)deviceId
24412445
[self throwError:@"[Leanplum start] must be called before calling deviceId"];
24422446
return nil;
24432447
}
2444-
return [LeanplumRequest deviceId];
2448+
return [LPAPIConfig sharedConfig].deviceId;
24452449
LP_END_TRY
24462450
return nil;
24472451
}
@@ -2453,7 +2457,7 @@ + (NSString *)userId
24532457
[self throwError:@"[Leanplum start] must be called before calling userId"];
24542458
return nil;
24552459
}
2456-
return [LeanplumRequest userId];
2460+
return [LPAPIConfig sharedConfig].userId;
24572461
LP_END_TRY
24582462
return nil;
24592463
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// LPAPIConfig.h
3+
// Leanplum-iOS-SDK-source
4+
//
5+
// Created by Mayank Sanganeria on 8/25/18.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
#import "LPRequesting.h"
10+
11+
@interface LPAPIConfig : NSObject
12+
13+
@property (nonatomic, readonly) NSString *appId;
14+
@property (nonatomic, readonly) NSString *accessKey;
15+
16+
@property (nonatomic, strong) NSString *deviceId;
17+
@property (nonatomic, strong) NSString *userId;
18+
@property (nonatomic, strong) NSString *token;
19+
20+
+ (instancetype)sharedConfig;
21+
22+
- (void)setAppId:(NSString *)appId withAccessKey:(NSString *)accessKey;
23+
24+
- (void)loadToken;
25+
- (void)saveToken;
26+
27+
@end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// LPAPIConfig.m
3+
// Leanplum-iOS-SDK-source
4+
//
5+
// Created by Mayank Sanganeria on 8/25/18.
6+
//
7+
8+
#import "LPAPIConfig.h"
9+
#import "LeanplumInternal.h"
10+
#import "LPResponse.h"
11+
#import "LPKeychainWrapper.h"
12+
#import "LPEventDataManager.h"
13+
#import "LPEventCallbackManager.h"
14+
15+
@interface LPAPIConfig()
16+
17+
@property (nonatomic, strong) NSString *appId;
18+
@property (nonatomic, strong) NSString *accessKey;
19+
20+
@end
21+
22+
23+
@implementation LPAPIConfig
24+
25+
+ (instancetype)sharedConfig {
26+
static LPAPIConfig *sharedConfig = nil;
27+
static dispatch_once_t onceToken;
28+
dispatch_once(&onceToken, ^{
29+
sharedConfig = [[self alloc] init];
30+
sharedConfig.token = nil;
31+
});
32+
return sharedConfig;
33+
}
34+
35+
- (void)setAppId:(NSString *)appId withAccessKey:(NSString *)accessKey
36+
{
37+
self.appId = appId;
38+
self.accessKey = accessKey;
39+
}
40+
41+
- (void)loadToken
42+
{
43+
NSError *err;
44+
NSString *token_ = [LPKeychainWrapper getPasswordForUsername:LP_KEYCHAIN_USERNAME
45+
andServiceName:LP_KEYCHAIN_SERVICE_NAME
46+
error:&err];
47+
if (!token_) {
48+
return;
49+
}
50+
51+
[self setToken:token_];
52+
}
53+
54+
- (void)saveToken
55+
{
56+
NSError *err;
57+
[LPKeychainWrapper storeUsername:LP_KEYCHAIN_USERNAME
58+
andPassword:[self token]
59+
forServiceName:LP_KEYCHAIN_SERVICE_NAME
60+
updateExisting:YES
61+
error:&err];
62+
}
63+
64+
@end

Leanplum-SDK/Classes/Managers/Networking/LeanplumRequest.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,9 @@
3737
BOOL _sent;
3838
}
3939

40-
+ (void)setAppId:(NSString *)appId withAccessKey:(NSString *)accessKey;
41-
+ (void)setDeviceId:(NSString *)deviceId;
42-
+ (void)setUserId:(NSString *)userId;
40+
+ (void)initializeStaticVars;
4341
+ (void)setUploadUrl:(NSString *)url;
4442

45-
+ (NSString *)deviceId;
46-
+ (NSString *)userId;
47-
+ (void)setToken:(NSString *)token;
48-
+ (void)loadToken;
49-
+ (void)saveToken;
50-
51-
+ (NSString *)appId;
52-
+ (NSString *)token;
53-
5443
- (void)attachApiKeys:(NSMutableDictionary *)dict;
5544

5645
- (id)initWithHttpMethod:(NSString *)httpMethod apiMethod:(NSString *)apiMethod

0 commit comments

Comments
 (0)