Skip to content

Commit 08fecfc

Browse files
authored
Merge pull request #1369 from BranchMetrics/SDK-2309
SDK-2309 cache link data when initialization is deferred
2 parents 980a0d5 + 2009c84 commit 08fecfc

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

Sources/BranchSDK/Branch.m

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate> {
157157
// This is enabled by setting deferInitForPluginRuntime to true in branch.json
158158
@property (nonatomic, assign, readwrite) BOOL deferInitForPluginRuntime;
159159
@property (nonatomic, copy, nullable) void (^cachedInitBlock)(void);
160+
@property (nonatomic, copy, readwrite) NSString *cachedURLString;
160161

161162
@end
162163

@@ -624,27 +625,20 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options
624625
[self.class addBranchSDKVersionToCrashlyticsReport];
625626
self.shouldAutomaticallyDeepLink = automaticallyDisplayController;
626627

627-
// If the SDK is already initialized, this means that initSession was called after other lifecycle calls.
628-
if (self.initializationStatus == BNCInitStatusInitialized) {
629-
[self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:nil];
630-
return;
631-
}
632-
633-
// Save data from push notification on app launch
628+
// Check for Branch link in a push payload
629+
NSString *pushURL = nil;
634630
#if !TARGET_OS_TV
635631
if ([options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
636632
id branchUrlFromPush = [options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey][BRANCH_PUSH_NOTIFICATION_PAYLOAD_KEY];
637633
if ([branchUrlFromPush isKindOfClass:[NSString class]]) {
638634
self.preferenceHelper.universalLinkUrl = branchUrlFromPush;
639635
self.preferenceHelper.referringURL = branchUrlFromPush;
636+
pushURL = (NSString *)branchUrlFromPush;
640637
}
641638
}
642639
#endif
643640

644-
// Handle case where there's no URI scheme or Universal Link.
645-
if (![options.allKeys containsObject:UIApplicationLaunchOptionsURLKey] && ![options.allKeys containsObject:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
646-
[self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:nil];
647-
}
641+
[self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:pushURL];
648642
}
649643

650644
- (void)setDeepLinkDebugMode:(NSDictionary *)debugParams {
@@ -1938,11 +1932,21 @@ - (void)initSafetyCheck {
19381932

19391933
- (void)initUserSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier urlString:(NSString *)urlString {
19401934

1941-
// ignore lifecycle calls while waiting for a plugin runtime.
19421935
@synchronized (self) {
19431936
if (self.deferInitForPluginRuntime) {
1944-
[[BranchLogger shared] logDebug:@"Branch init is deferred, ignoring init call." error:nil];
1937+
if (urlString) {
1938+
[[BranchLogger shared] logDebug:@"Branch init is deferred, caching link" error:nil];
1939+
self.cachedURLString = urlString;
1940+
} else {
1941+
[[BranchLogger shared] logDebug:@"Branch init is deferred, ignoring lifecycle call without a link" error:nil];
1942+
}
19451943
return;
1944+
} else {
1945+
if (!urlString && self.cachedURLString) {
1946+
urlString = self.cachedURLString;
1947+
[[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Using cached link: %@", urlString] error:nil];
1948+
}
1949+
self.cachedURLString = nil;
19461950
}
19471951
}
19481952

0 commit comments

Comments
 (0)