Skip to content

Commit 2e3fba2

Browse files
authored
Removed Auto Initialization (#1571)
- Added API - `- (void)setConsumerProtectionAttributionLevel:(BranchAttributionLevel)level resetSession:(BOOL)resetSession` to provide an option to disable resetting session. - Remove silent SDK initialization - initSafetyCheck removed.Now APIs will return or log BNCInitError error. - Added @synchronized(self) around all 5 init status checks to ensure it waits for any in-progress initSession code (which also uses @synchronized(self)) to finish and update the status before evaluating.
1 parent e57b69e commit 2e3fba2

3 files changed

Lines changed: 75 additions & 21 deletions

File tree

Sources/BranchSDK/Branch.m

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,10 @@ + (void)setAnonID:(NSString *)anonID {
690690
}
691691

692692
- (void)setConsumerProtectionAttributionLevel:(BranchAttributionLevel)level {
693+
[self setConsumerProtectionAttributionLevel:level resetSession:YES];
694+
}
695+
696+
- (void)setConsumerProtectionAttributionLevel:(BranchAttributionLevel)level resetSession:(BOOL)resetSession {
693697
self.preferenceHelper.attributionLevel = level;
694698

695699
[[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Setting Consumer Protection Attribution Level to %@", level] error:nil];
@@ -720,8 +724,10 @@ - (void)setConsumerProtectionAttributionLevel:(BranchAttributionLevel)level {
720724
// Set the flag:
721725
[BNCPreferenceHelper sharedInstance].trackingDisabled = NO;
722726

723-
// Initialize a Branch session:
724-
[[Branch getInstance] initUserSessionAndCallCallback:NO sceneIdentifier:nil urlString:nil reset:true];
727+
if (resetSession) {
728+
// Initialize a Branch session:
729+
[[Branch getInstance] initUserSessionAndCallCallback:NO sceneIdentifier:nil urlString:nil reset:true];
730+
}
725731
}
726732
}
727733

@@ -1220,7 +1226,16 @@ - (void)logoutWithCallback:(callbackWithStatus)callback {
12201226
}
12211227

12221228
- (void)sendServerRequest:(BNCServerRequest*)request {
1223-
[self initSafetyCheck];
1229+
@synchronized (self) {
1230+
if (self.initializationStatus == BNCInitStatusUninitialized) {
1231+
NSError *error = [NSError branchErrorWithCode:BNCInitError];
1232+
[[BranchLogger shared] logWarning:@"Branch SDK is not initialized, cannot send this request. Please intialize session before calling this API." error:error];
1233+
dispatch_async(dispatch_get_main_queue(), ^{
1234+
[[BNCCallbackMap shared] callCompletionForRequest:request withSuccessStatus:NO error:error];
1235+
});
1236+
return;
1237+
}
1238+
}
12241239
dispatch_async(self.isolationQueue, ^(){
12251240
[self.requestQueue enqueue:request];
12261241
[self processNextQueueItem];
@@ -1298,7 +1313,16 @@ - (BranchLinkProperties *)getLatestReferringBranchLinkProperties {
12981313
#pragma mark - Query methods
12991314

13001315
- (void)lastAttributedTouchDataWithAttributionWindow:(NSInteger)window completion:(void(^) (BranchLastAttributedTouchData * _Nullable latd, NSError * _Nullable error))completion {
1301-
[self initSafetyCheck];
1316+
@synchronized (self) {
1317+
if (self.initializationStatus == BNCInitStatusUninitialized) {
1318+
NSError *error = [NSError branchErrorWithCode:BNCInitError];
1319+
[[BranchLogger shared] logWarning:@"Branch SDK is not initialized, cannot request LATD. Please intialize session before calling this API." error:error];
1320+
dispatch_async(dispatch_get_main_queue(), ^{
1321+
if (completion) { completion(nil, error); }
1322+
});
1323+
return;
1324+
}
1325+
}
13021326
dispatch_async(self.isolationQueue, ^(){
13031327
[BranchLastAttributedTouchData requestLastTouchAttributedData:self.serverInterface key:self.class.branchKey attributionWindow:window completion:completion];
13041328
});
@@ -1419,7 +1443,16 @@ - (void)getShortUrlWithParams:(NSDictionary *)params andTags:(NSArray *)tags and
14191443
}
14201444

14211445
- (void)getSpotlightUrlWithParams:(NSDictionary *)params callback:(callbackWithParams)callback {
1422-
[self initSafetyCheck];
1446+
@synchronized (self) {
1447+
if (self.initializationStatus == BNCInitStatusUninitialized) {
1448+
NSError *error = [NSError branchErrorWithCode:BNCInitError];
1449+
[[BranchLogger shared] logWarning:@"Branch SDK is not initialized, cannot create Spotlight URL. Please intialize session before calling this API." error:error];
1450+
dispatch_async(dispatch_get_main_queue(), ^{
1451+
if (callback) { callback(nil, error); }
1452+
});
1453+
return;
1454+
}
1455+
}
14231456
dispatch_async(self.isolationQueue, ^(){
14241457
BranchSpotlightUrlRequest *req = [[BranchSpotlightUrlRequest alloc] initWithParams:params callback:callback];
14251458
[self.requestQueue enqueue:req];
@@ -1683,7 +1716,18 @@ - (void)generateShortUrl:(NSArray *)tags
16831716
andCampaign:campaign andParams:(NSDictionary *)params
16841717
andCallback:(callbackWithUrl)callback {
16851718

1686-
[self initSafetyCheck];
1719+
@synchronized (self) {
1720+
if (self.initializationStatus == BNCInitStatusUninitialized) {
1721+
NSError *error = [NSError branchErrorWithCode:BNCInitError];
1722+
[[BranchLogger shared] logWarning:@"Branch SDK is not initialized, cannot generate short URL. Please intialize session before calling this API." error:error];
1723+
if (callback) {
1724+
dispatch_async(dispatch_get_main_queue(), ^{
1725+
callback(nil, error);
1726+
});
1727+
}
1728+
return;
1729+
}
1730+
}
16871731
dispatch_async(self.isolationQueue, ^(){
16881732
BNCLinkData *linkData = [self prepareLinkDataFor:tags
16891733
andAlias:alias
@@ -1891,7 +1935,18 @@ - (BNCLinkData *)prepareLinkDataFor:(NSArray *)tags
18911935
#pragma mark - BranchUniversalObject methods
18921936

18931937
- (void)registerViewWithParams:(NSDictionary *)params andCallback:(callbackWithParams)callback {
1894-
[self initSafetyCheck];
1938+
@synchronized (self) {
1939+
if (self.initializationStatus == BNCInitStatusUninitialized) {
1940+
NSError *error = [NSError branchErrorWithCode:BNCInitError];
1941+
[[BranchLogger shared] logWarning:@"Branch SDK is not initialized, cannot register view. Please intialize session before calling this API." error:error];
1942+
if (callback) {
1943+
dispatch_async(dispatch_get_main_queue(), ^{
1944+
callback(nil, error);
1945+
});
1946+
}
1947+
return;
1948+
}
1949+
}
18951950
dispatch_async(self.isolationQueue, ^(){
18961951
BranchUniversalObject *buo = [[BranchUniversalObject alloc] init];
18971952
buo.contentMetadata.customMetadata = (id) params;
@@ -2151,14 +2206,6 @@ - (void)notifyNativeToInit {
21512206
self.cachedInitBlock = nil;
21522207
}
21532208

2154-
// SDK-631 Workaround to maintain existing error handling behavior.
2155-
// Some methods require init before they are called. Instead of returning an error, we try to fix the situation by calling init ourselves.
2156-
- (void)initSafetyCheck {
2157-
if (self.initializationStatus == BNCInitStatusUninitialized) {
2158-
[[BranchLogger shared] logDebug:@"Branch avoided an error by preemptively initializing." error:nil];
2159-
[self initUserSessionAndCallCallback:NO sceneIdentifier:nil urlString:nil reset:NO];
2160-
}
2161-
}
21622209

21632210
- (void)initUserSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier urlString:(NSString *)urlString reset:(BOOL)reset {
21642211

Sources/BranchSDK/Public/Branch.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,15 @@ extern BranchAttributionLevel const BranchAttributionLevelNone;
941941
*/
942942
- (void)setConsumerProtectionAttributionLevel:(BranchAttributionLevel)level;
943943

944+
/**
945+
Sets the consumer protection attribution level with an option to reset the session.
946+
947+
@param level The desired consumer protection attribution level, represented by the BranchAttributionLevel enum (Full, Reduced, Minimal, None).
948+
@param resetSession If YES, a new session will be initialized when transitioning from BranchAttributionLevelNone to other higher levels.
949+
If NO, the session will not be re-initialized automatically when transitioning from BranchAttributionLevelNone to other higher levels.
950+
*/
951+
- (void)setConsumerProtectionAttributionLevel:(BranchAttributionLevel)level resetSession:(BOOL)resetSession;
952+
944953

945954
#pragma mark - Session Item methods
946955

Sources/BranchSDK/Public/BranchEvent.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,18 @@ typedef NS_ENUM(NSInteger, BranchEventAdType) {
9797
/**
9898
Logs the event on the Branch server.
9999
This version will callback on success/failure.
100-
100+
101101
This method should only be invoked after initSession.
102-
If it is invoked before, then we will silently initialize the SDK before the callback has been set, in order to carry out this method's required task.
103-
As a result, you may experience issues where the initSession callback does not fire. Again, the solution to this issue is to only invoke this method after you have invoked initSession.
102+
If invoked before initSession, the event will be dropped and a BNCInitError will be returned.
104103
*/
105104
- (void)logEventWithCompletion:(void (^_Nullable)(BOOL success, NSError * _Nullable error))completion;
106105

107106
/**
108107
Logs the event on the Branch server.
109108
This version automatically caches and retries as necessary.
110-
109+
111110
This method should only be invoked after initSession.
112-
If it is invoked before, then we will silently initialize the SDK before the callback has been set, in order to carry out this method's required task.
113-
As a result, you may experience issues where the initSession callback does not fire. Again, the solution to this issue is to only invoke this method after you have invoked initSession.
111+
If invoked before initSession, the event will be dropped.
114112
*/
115113
- (void)logEvent;
116114

0 commit comments

Comments
 (0)