Skip to content

Commit 3582e57

Browse files
authored
Refactor SKAN related code and BranchOpenRequest Class (#1570)
* Refactoring BranchOpenRequest Class * Refactoring BranchEvent Class * SKAN logic is moved in BNCSKAdNetwork
1 parent a8fa6c1 commit 3582e57

8 files changed

Lines changed: 257 additions & 181 deletions

File tree

BranchSDK.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@
500500
E7FAF6A02E26E497006C167F /* BranchConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = E7FAF69F2E26E497006C167F /* BranchConstants.h */; settings = {ATTRIBUTES = (Public, ); }; };
501501
E7FAF6A12E26E497006C167F /* BranchConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = E7FAF69F2E26E497006C167F /* BranchConstants.h */; settings = {ATTRIBUTES = (Public, ); }; };
502502
E7FAF6A22E26E497006C167F /* BranchConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = E7FAF69F2E26E497006C167F /* BranchConstants.h */; settings = {ATTRIBUTES = (Public, ); }; };
503+
BB00000001000000000000B1 /* BranchConfigurationController.h in Headers */ = {isa = PBXBuildFile; fileRef = BB00000001000000000000A1 /* BranchConfigurationController.h */; };
504+
BB00000002000000000000B1 /* BranchConfigurationController.h in Headers */ = {isa = PBXBuildFile; fileRef = BB00000001000000000000A1 /* BranchConfigurationController.h */; };
505+
BB00000003000000000000B1 /* BranchConfigurationController.h in Headers */ = {isa = PBXBuildFile; fileRef = BB00000001000000000000A1 /* BranchConfigurationController.h */; };
506+
BB00000004000000000000B1 /* BranchConfigurationController.m in Sources */ = {isa = PBXBuildFile; fileRef = BB00000004000000000000A1 /* BranchConfigurationController.m */; };
507+
BB00000005000000000000B1 /* BranchConfigurationController.m in Sources */ = {isa = PBXBuildFile; fileRef = BB00000004000000000000A1 /* BranchConfigurationController.m */; };
508+
BB00000006000000000000B1 /* BranchConfigurationController.m in Sources */ = {isa = PBXBuildFile; fileRef = BB00000004000000000000A1 /* BranchConfigurationController.m */; };
503509
/* End PBXBuildFile section */
504510

505511
/* Begin PBXContainerItemProxy section */
@@ -705,6 +711,8 @@
705711
E7F311AD2DACB4D400F824A7 /* BNCODMInfoCollector.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCODMInfoCollector.m; sourceTree = "<group>"; };
706712
E7F311B02DACB54100F824A7 /* BNCODMInfoCollector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCODMInfoCollector.h; sourceTree = "<group>"; };
707713
E7FAF69F2E26E497006C167F /* BranchConstants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BranchConstants.h; sourceTree = "<group>"; };
714+
BB00000001000000000000A1 /* BranchConfigurationController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BranchConfigurationController.h; sourceTree = "<group>"; };
715+
BB00000004000000000000A1 /* BranchConfigurationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BranchConfigurationController.m; sourceTree = "<group>"; };
708716
/* End PBXFileReference section */
709717

710718
/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */

Sources/BranchSDK/BNCSKAdNetwork.m

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,75 @@ - (BOOL) shouldCallPostbackForDataResponse:(NSDictionary *) dataResponseDictiona
218218
return shouldCallUpdatePostback;
219219
}
220220

221+
- (void)updateConversionValueFromResponse:(NSDictionary *)responseData {
222+
if (![responseData[BRANCH_RESPONSE_KEY_UPDATE_CONVERSION_VALUE] isKindOfClass:NSNumber.class]) {
223+
return;
224+
}
225+
226+
NSNumber *conversionValue = (NSNumber *)responseData[BRANCH_RESPONSE_KEY_UPDATE_CONVERSION_VALUE];
227+
if (!conversionValue || ![BNCPreferenceHelper sharedInstance].invokeRegisterApp) {
228+
return;
229+
}
230+
231+
void (^completionHandler)(NSError *) = ^(NSError * _Nullable error) {
232+
if (error) {
233+
[[BranchLogger shared] logError:[NSString stringWithFormat:@"Update conversion value failed with error - %@", [error description]] error:error];
234+
} else {
235+
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Update conversion value was successful. Conversion Value - %@", conversionValue] error:nil];
236+
}
237+
};
238+
239+
if (@available(iOS 16.1, macCatalyst 16.1, *)) {
240+
NSString *coarseConversionValue = [self getCoarseConversionValueFromDataResponse:responseData];
241+
BOOL lockWin = [self getLockedStatusFromDataResponse:responseData];
242+
BOOL shouldCallUpdatePostback = [self shouldCallPostbackForDataResponse:responseData];
243+
244+
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"SKAN 4.0 params - conversionValue:%@ coarseValue:%@, locked:%d, shouldCallPostback:%d, currentWindow:%d, firstAppLaunchTime: %@", conversionValue, coarseConversionValue, lockWin, shouldCallUpdatePostback, (int)[BNCPreferenceHelper sharedInstance].skanCurrentWindow, [BNCPreferenceHelper sharedInstance].firstAppLaunchTime] error:nil];
245+
246+
if (shouldCallUpdatePostback) {
247+
[self updatePostbackConversionValue:conversionValue.longValue coarseValue:coarseConversionValue lockWindow:lockWin completionHandler:completionHandler];
248+
}
249+
} else if (@available(iOS 15.4, macCatalyst 15.4, *)) {
250+
[self updatePostbackConversionValue:conversionValue.intValue completionHandler:completionHandler];
251+
} else {
252+
[self updateConversionValue:conversionValue.integerValue];
253+
}
254+
}
255+
256+
- (void (^)(NSError *))skanCompletionHandlerWithSuccessDescription:(NSString *)description {
257+
return ^(NSError * _Nullable error) {
258+
if (error) {
259+
[[BranchLogger shared] logError:[NSString stringWithFormat:@"Update conversion value failed with error - %@", [error description]] error:error];
260+
} else {
261+
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Update conversion value was successful for %@", description] error:nil];
262+
}
263+
};
264+
}
265+
266+
- (void)registerAndUpdateConversionFromResponse:(NSDictionary *)data isInstall:(BOOL)isInstall {
267+
BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance];
268+
269+
if ([data[BRANCH_RESPONSE_KEY_INVOKE_REGISTER_APP] isKindOfClass:NSNumber.class]) {
270+
NSNumber *invokeRegister = (NSNumber *)data[BRANCH_RESPONSE_KEY_INVOKE_REGISTER_APP];
271+
preferenceHelper.invokeRegisterApp = invokeRegister.boolValue;
272+
if (invokeRegister.boolValue && isInstall) {
273+
void (^completionHandler)(NSError *) = [self skanCompletionHandlerWithSuccessDescription:@"INSTALL Event"];
274+
if (@available(iOS 16.1, macCatalyst 16.1, *)){
275+
NSString *defaultCoarseConValue = [self getCoarseConversionValueFromDataResponse:@{}];
276+
[self updatePostbackConversionValue:0 coarseValue:defaultCoarseConValue
277+
lockWindow:NO completionHandler:completionHandler];
278+
} else if (@available(iOS 15.4, macCatalyst 15.4, *)){
279+
[self updatePostbackConversionValue:0 completionHandler:completionHandler];
280+
}
281+
else {
282+
[self registerAppForAdNetworkAttribution];
283+
}
284+
}
285+
} else {
286+
preferenceHelper.invokeRegisterApp = NO;
287+
}
288+
}
289+
221290
- (BOOL)isSKANAllowedForAttributionLevel {
222291
BranchAttributionLevel level = [[BNCPreferenceHelper sharedInstance] attributionLevel];
223292
return !([level isEqualToString:BranchAttributionLevelMinimal] ||

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/BranchEvent.m

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#import "BNCReachability.h"
1515
#import "BNCSKAdNetwork.h"
1616
#import "BNCPartnerParameters.h"
17-
#import "BNCPreferenceHelper.h"
1817
#import "BNCEventUtils.h"
1918
#import "BNCRequestFactory.h"
2019
#import "BNCServerAPI.h"
@@ -88,38 +87,8 @@ - (void)processResponse:(BNCServerResponse*)response error:(NSError*)error {
8887
? (NSDictionary*) response.data : nil;
8988

9089
#if !TARGET_OS_TV
91-
if (dictionary && [dictionary[BRANCH_RESPONSE_KEY_UPDATE_CONVERSION_VALUE] isKindOfClass:NSNumber.class]) {
92-
NSNumber *conversionValue = (NSNumber *)dictionary[BRANCH_RESPONSE_KEY_UPDATE_CONVERSION_VALUE];
93-
// Regardless of SKAN opted-in in dashboard, we always get conversionValue, so adding check to find out if install/open response had "invoke_register_app" true
94-
if (conversionValue && [BNCPreferenceHelper sharedInstance].invokeRegisterApp) {
95-
if (@available(iOS 16.1, macCatalyst 16.1, *)){
96-
NSString * coarseConversionValue = [[BNCSKAdNetwork sharedInstance] getCoarseConversionValueFromDataResponse:dictionary] ;
97-
BOOL lockWin = [[BNCSKAdNetwork sharedInstance] getLockedStatusFromDataResponse:dictionary];
98-
BOOL shouldCallUpdatePostback = [[BNCSKAdNetwork sharedInstance] shouldCallPostbackForDataResponse:dictionary];
99-
100-
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"SKAN 4.0 params - conversionValue:%@ coarseValue:%@, locked:%d, shouldCallPostback:%d, currentWindow:%d, firstAppLaunchTime: %@", conversionValue, coarseConversionValue, lockWin, shouldCallUpdatePostback, (int)[BNCPreferenceHelper sharedInstance].skanCurrentWindow, [BNCPreferenceHelper sharedInstance].firstAppLaunchTime] error:nil];
101-
if(shouldCallUpdatePostback){
102-
[[BNCSKAdNetwork sharedInstance] updatePostbackConversionValue: conversionValue.longValue coarseValue:coarseConversionValue lockWindow:lockWin completionHandler:^(NSError * _Nullable error) {
103-
if (error) {
104-
[[BranchLogger shared] logError:[NSString stringWithFormat:@"Update conversion value failed with error - %@", [error description]] error:error];
105-
} else {
106-
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Update conversion value was successful. Conversion Value - %@", conversionValue] error:nil];
107-
}
108-
}];
109-
}
110-
111-
} else if (@available(iOS 15.4, macCatalyst 15.4, *)) {
112-
[[BNCSKAdNetwork sharedInstance] updatePostbackConversionValue:conversionValue.intValue completionHandler: ^(NSError *error){
113-
if (error) {
114-
[[BranchLogger shared] logError:[NSString stringWithFormat:@"Update conversion value failed with error - %@", [error description]] error:error];
115-
} else {
116-
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Update conversion value was successful. Conversion Value - %@", conversionValue] error:nil];
117-
}
118-
}];
119-
} else {
120-
[[BNCSKAdNetwork sharedInstance] updateConversionValue:conversionValue.integerValue];
121-
}
122-
}
90+
if (dictionary) {
91+
[[BNCSKAdNetwork sharedInstance] updateConversionValueFromResponse:dictionary];
12392
}
12493
#endif
12594

0 commit comments

Comments
 (0)