Skip to content

Commit 45a2d21

Browse files
[SDK-1944] feat(install): Add Developer ID to v1/install
1 parent 08e8940 commit 45a2d21

File tree

5 files changed

+104
-11
lines changed

5 files changed

+104
-11
lines changed

Branch-TestBed/Branch-SDK-Tests/BranchSetIdentityRequestTests.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,25 @@
1515
static NSString * const IDENTITY_TEST_USER_ID = @"foo_id";
1616

1717
@interface BranchSetIdentityRequestTests : BNCTestCase
18+
//@property (nonatomic, strong) Branch *branch;
19+
//@property (nonatomic, strong) BNCPreferenceHelper *preferenceHelper;
1820
@end
1921

2022
@implementation BranchSetIdentityRequestTests
2123

24+
//- (void)setUp {
25+
// [super setUp];
26+
// self.branch = [Branch getInstance];
27+
// self.preferenceHelper = [BNCPreferenceHelper sharedInstance];
28+
// [self.branch logout];
29+
//}
30+
//
31+
//- (void)tearDown {
32+
// self.branch = nil;
33+
// self.preferenceHelper = nil;
34+
// [super tearDown];
35+
//}
36+
2237
- (void)testRequestBody {
2338
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance];
2439
NSDictionary * const expectedParams = @{
@@ -178,4 +193,52 @@ - (void)testEmptyResponseFieldsAfterSetIdentity {
178193
XCTAssertNotNil(preferenceHelper.randomizedBundleToken);
179194
}
180195

196+
#pragma mark - setIdentity Tests
197+
- (void)testSetIdentityWithCallback {
198+
Branch *branch = [Branch getInstance];
199+
[branch logoutWithCallback:^(BOOL changed, NSError * _Nullable error) {
200+
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper new];
201+
202+
XCTestExpectation *expectation = [self expectationWithDescription:@"setIdentity callback is called"];
203+
204+
[branch setIdentity:@"testUserIdWithCallback" withCallback:^(NSDictionary *params, NSError *error) {
205+
XCTAssertEqualObjects(@"testUserIdWithCallback", preferenceHelper.userIdentity);
206+
[expectation fulfill];
207+
}];
208+
209+
[self waitForExpectationsWithTimeout:5 handler:nil];
210+
}];
211+
}
212+
213+
- (void)testSetIdentityWithNilUserId {
214+
Branch *branch = [Branch getInstance];
215+
[branch logoutWithCallback:^(BOOL changed, NSError * _Nullable error) {
216+
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper new];
217+
218+
XCTestExpectation *expectation = [self expectationWithDescription:@"setIdentityWithNil callback is called"];
219+
220+
[branch setIdentity:nil withCallback:^(NSDictionary *params, NSError *error) {
221+
XCTAssertNil(preferenceHelper.userIdentity);
222+
[expectation fulfill];
223+
}];
224+
225+
[self waitForExpectationsWithTimeout:5 handler:nil];
226+
}];
227+
228+
229+
}
230+
231+
- (void)testSetIdentityWithUserId {
232+
Branch *branch = [Branch getInstance];
233+
[branch logoutWithCallback:^(BOOL changed, NSError * _Nullable error) {
234+
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper new];
235+
236+
NSString *testUserId = @"testUserId";
237+
[branch setIdentity:testUserId withCallback:nil];
238+
239+
XCTAssertEqualObjects(@"testUserId", preferenceHelper.userIdentity);
240+
}];
241+
242+
}
243+
181244
@end

Branch-TestBed/Branch-TestBed/AppDelegate.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ - (BOOL)application:(UIApplication *)application
5151

5252
[branch checkPasteboardOnInstall];
5353

54-
5554
/*
5655
* Required: Initialize Branch, passing a deep link handler block:
5756
*/
5857

5958
[self setLogFile:@"OpenNInstall"];
60-
// [branch setIdentity:@"Bobby Branch"];
61-
62-
[branch initSessionWithLaunchOptions:launchOptions
63-
andRegisterDeepLinkHandlerUsingBranchUniversalObject:
64-
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
59+
60+
[branch setIdentity:@"Bobby Branch"];
61+
62+
[branch initSessionWithLaunchOptions:launchOptions andRegisterDeepLinkHandlerUsingBranchUniversalObject:
63+
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
64+
6565
[self setLogFile:nil];
66-
[self handleDeepLinkObject:universalObject linkProperties:linkProperties error:error];
66+
[self handleDeepLinkObject:universalObject linkProperties:linkProperties error:error];
6767
}];
6868

6969
// Push notification support (Optional)

BranchSDK/BNCServerInterface.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,12 @@ - (NSMutableDictionary *)prepareParamDict:(NSDictionary *)params
465465

466466
if ([self.requestEndpoint containsString:@"/v1/open"]) {
467467
[fullParamDict bnc_safeSetObject:[BNCPreferenceHelper sharedInstance].userIdentity forKey:@"identity"];
468-
}
468+
}
469+
470+
if ([self.requestEndpoint containsString:@"/v1/install"]) {
471+
[fullParamDict bnc_safeSetObject: [Branch getInstance].installUserId forKey:@"identity"];
472+
}
473+
469474

470475
return fullParamDict;
471476
}

BranchSDK/Branch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,9 @@ typedef NS_ENUM(NSUInteger, BranchCreditHistoryOrder) {
18561856
- (void)passPasteItemProviders:(NSArray<NSItemProvider *> *)itemProviders API_AVAILABLE(ios(16));
18571857
#endif
18581858

1859+
@property (copy, nonatomic, nullable) NSString *installUserId;
1860+
@property (copy, nonatomic, nullable) callbackWithParams setIdentityCallback;
1861+
18591862
@end
18601863

18611864
NS_ASSUME_NONNULL_END

BranchSDK/Branch.m

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ - (void)registerDeepLinkController:(UIViewController <BranchDeepLinkingControlle
10471047
#pragma mark - Identity methods
10481048

10491049
- (void)setIdentity:(NSString *)userId {
1050-
[self setIdentity:userId withCallback:NULL];
1050+
[self setIdentity:userId withCallback: nil];
10511051
}
10521052

10531053
- (void)setIdentity:(NSString *)userId withCallback:(callbackWithParams)callback {
@@ -1057,20 +1057,40 @@ - (void)setIdentity:(NSString *)userId withCallback:(callbackWithParams)callback
10571057
}
10581058
return;
10591059
}
1060+
1061+
if (self.initializationStatus == BNCInitStatusUninitialized ) {
1062+
[self cacheIdentity:userId withCallback:callback];
1063+
} else {
1064+
[self sendIdentity:userId withCallback:callback];
1065+
}
1066+
}
10601067

1061-
[self initSafetyCheck];
1068+
- (void) sendIdentity:(NSString *)userId withCallback:(callbackWithParams)callback {
10621069
dispatch_async(self.isolationQueue, ^(){
10631070
BranchSetIdentityRequest *req = [[BranchSetIdentityRequest alloc] initWithUserId:userId callback:callback];
10641071
[self.requestQueue enqueue:req];
10651072
[self processNextQueueItem];
10661073
});
10671074
}
10681075

1076+
- (void) cacheIdentity: (NSString *)userId withCallback:(callbackWithParams)callback {
1077+
self.installUserId = userId;
1078+
self.setIdentityCallback = callback;
1079+
}
1080+
1081+
- (void) applySavedIdentity {
1082+
if (self.installUserId != nil) {
1083+
[self sendIdentity:self.installUserId withCallback:self.setIdentityCallback];
1084+
1085+
self.installUserId = nil;
1086+
self.setIdentityCallback = nil;
1087+
}
1088+
}
1089+
10691090
- (void)logout {
10701091
[self logoutWithCallback:nil];
10711092
}
10721093

1073-
10741094
- (void)logoutWithCallback:(callbackWithStatus)callback {
10751095
if (self.initializationStatus == BNCInitStatusUninitialized) {
10761096
NSError *error =
@@ -2285,6 +2305,8 @@ - (void)handleInitSuccessAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSS
22852305
}
22862306
[self sendOpenNotificationWithLinkParameters:latestReferringParams error:nil];
22872307

2308+
[self applySavedIdentity];
2309+
22882310
if (!self.urlFilter.hasUpdatedPatternList) {
22892311
[self.urlFilter updatePatternListWithCompletion:nil];
22902312
}

0 commit comments

Comments
 (0)