Skip to content

Commit 2a59d69

Browse files
authored
Merge pull request #1233 from BranchMetrics/SDK-1831-update-branch.json-support
SDK-1831 update branch.json support
2 parents a229896 + cc3921e commit 2a59d69

File tree

6 files changed

+82
-34
lines changed

6 files changed

+82
-34
lines changed

BranchSDK/Branch.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,16 @@ typedef NS_ENUM(NSUInteger, BranchCreditHistoryOrder) {
528528
*/
529529
- (void)dispatchToIsolationQueue:(dispatch_block_t)initBlock;
530530

531+
/**
532+
DO NOT USE unless you are implementing deferred initialization for plugins.
533+
534+
Platforms such as React Native and Unity, have slow runtime startups. This results in early lifecycle events before client code can run.
535+
When `deferInitForPlugin` is true in `branch.json` initSession with cache itself until this method is called.
536+
537+
Note that while init is deferred, other calls to the Branch SDK may result in errors. For that reason, do not use this feature for general SDK init deferral.
538+
*/
539+
- (void)notifyNativeToInit;
540+
531541
#pragma mark - Push Notification support
532542

533543
/**

BranchSDK/Branch.m

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate> {
152152

153153
@property (nonatomic, copy, nullable) void (^sceneSessionInitWithCallback)(BNCInitSessionResponse * _Nullable initResponse, NSError * _Nullable error);
154154

155+
// Support for deferred SDK initialization. Used to support slow plugin runtime startup.
156+
// This is enabled by setting deferInitForPluginRuntime to true in branch.json
157+
@property (nonatomic, assign, readwrite) BOOL deferInitForPluginRuntime;
158+
@property (nonatomic, copy, nullable) void (^cachedInitBlock)(void);
159+
155160
@end
156161

157162
@implementation Branch
@@ -226,13 +231,14 @@ - (id)initWithInterface:(BNCServerInterface *)interface
226231
[self loadUserAgent];
227232

228233
BranchJsonConfig *config = BranchJsonConfig.instance;
234+
self.deferInitForPluginRuntime = config.deferInitForPluginRuntime;
229235

230-
if (config.checkPasteboardOnInstall) {
231-
[self checkPasteboardOnInstall];
236+
if (config.enableLogging) {
237+
[self enableLogging];
232238
}
233239

234-
if (config.delayInitToCheckForSearchAds) {
235-
[self delayInitToCheckForSearchAds];
240+
if (config.checkPasteboardOnInstall) {
241+
[self checkPasteboardOnInstall];
236242
}
237243

238244
if (config.enableFacebookLinkCheck) {
@@ -613,8 +619,10 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)
613619

614620
- (void)initSceneSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)isReferrable explicitlyRequestedReferrable:(BOOL)explicitlyRequestedReferrable automaticallyDisplayController:(BOOL)automaticallyDisplayController
615621
registerDeepLinkHandler:(void (^)(BNCInitSessionResponse * _Nullable initResponse, NSError * _Nullable error))callback {
616-
self.sceneSessionInitWithCallback = callback;
617-
[self initSessionWithLaunchOptions:options isReferrable:isReferrable explicitlyRequestedReferrable:explicitlyRequestedReferrable automaticallyDisplayController:automaticallyDisplayController];
622+
[self deferInitBlock:^{
623+
self.sceneSessionInitWithCallback = callback;
624+
[self initSessionWithLaunchOptions:options isReferrable:isReferrable explicitlyRequestedReferrable:explicitlyRequestedReferrable automaticallyDisplayController:automaticallyDisplayController];
625+
}];
618626
}
619627

620628
- (void)initSessionWithLaunchOptions:(NSDictionary *)options
@@ -2136,9 +2144,36 @@ - (void)clearNetworkQueue {
21362144

21372145
#pragma mark - Session Initialization
21382146

2147+
// Defers block until notifyNativeToInit is called.
2148+
- (BOOL)deferInitBlock:(void (^)(void))block {
2149+
BOOL deferred = NO;
2150+
@synchronized (self) {
2151+
if (self.deferInitForPluginRuntime) {
2152+
self.cachedInitBlock = block;
2153+
deferred = YES;
2154+
}
2155+
}
2156+
2157+
if (!deferred && block) {
2158+
block();
2159+
}
2160+
return deferred;
2161+
}
2162+
2163+
// Releases deferred init block
2164+
- (void)notifyNativeToInit {
2165+
@synchronized (self) {
2166+
self.deferInitForPluginRuntime = NO;
2167+
}
2168+
2169+
if (self.cachedInitBlock) {
2170+
self.cachedInitBlock();
2171+
}
2172+
self.cachedInitBlock = nil;
2173+
}
2174+
21392175
// SDK-631 Workaround to maintain existing error handling behavior.
21402176
// Some methods require init before they are called. Instead of returning an error, we try to fix the situation by calling init ourselves.
2141-
// There is a follow up ticket to improve this. SDK-633
21422177
- (void)initSafetyCheck {
21432178
if (self.initializationStatus == BNCInitStatusUninitialized) {
21442179
BNCLogDebug(@"Branch avoided an error by preemptively initializing.");
@@ -2147,6 +2182,16 @@ - (void)initSafetyCheck {
21472182
}
21482183

21492184
- (void)initUserSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier {
2185+
2186+
// ignore lifecycle calls while waiting for a plugin runtime.
2187+
@synchronized (self) {
2188+
if (self.deferInitForPluginRuntime) {
2189+
//NSString *debug = [NSString stringWithFormat:@"Init is deferred, ignoring call: %@", NSThread.callStackSymbols];
2190+
//BNCLogDebug(debug);
2191+
return;
2192+
}
2193+
}
2194+
21502195
dispatch_async(self.isolationQueue, ^(){
21512196
NSString *urlstring = nil;
21522197
if (self.preferenceHelper.universalLinkUrl.length) {

BranchSDK/BranchJsonConfig.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ extern NSString * _Nonnull const BranchJsonConfigBranchKeyOption;
1313
extern NSString * _Nonnull const BranchJsonConfigLiveKeyOption;
1414
extern NSString * _Nonnull const BranchJsonConfigTestKeyOption;
1515
extern NSString * _Nonnull const BranchJsonConfigUseTestInstanceOption;
16-
extern NSString * _Nonnull const BranchJsonConfigDelayInitToCheckForSearchAdsOption;
17-
extern NSString * _Nonnull const BranchJsonConfigAppleSearchAdsDebugModeOption;
18-
extern NSString * _Nonnull const BranchJsonConfigDeferInitializationForJSLoadOption;
16+
extern NSString * _Nonnull const BranchJsonConfigDeferInitForPluginRuntimeOption;
17+
extern NSString * _Nonnull const BranchJsonConfigEnableLogging;
1918
extern NSString * _Nonnull const BranchJsonConfigEnableFacebookLinkCheck;
2019
extern NSString * _Nonnull const BranchJsonConfigCheckPasteboardOnInstall;
2120

@@ -28,9 +27,8 @@ extern NSString * _Nonnull const BranchJsonConfigCheckPasteboardOnInstall;
2827
@property (nonatomic, readonly, nullable, copy) NSString *liveKey;
2928
@property (nonatomic, readonly, nullable, copy) NSString *testKey;
3029
@property (nonatomic, readonly, assign) BOOL useTestInstance;
31-
@property (nonatomic, readonly, assign) BOOL delayInitToCheckForSearchAds;
32-
@property (nonatomic, readonly, assign) BOOL appleSearchAdsDebugMode;
33-
@property (nonatomic, readonly, assign) BOOL deferInitializationForJSLoad;
30+
@property (nonatomic, readonly, assign) BOOL deferInitForPluginRuntime;
31+
@property (nonatomic, readonly, assign) BOOL enableLogging;
3432
@property (nonatomic, readonly, assign) BOOL enableFacebookLinkCheck;
3533
@property (nonatomic, readonly, assign) BOOL checkPasteboardOnInstall;
3634

BranchSDK/BranchJsonConfig.m

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
NSString * _Nonnull const BranchJsonConfigLiveKeyOption = @"liveKey";
1515
NSString * _Nonnull const BranchJsonConfigTestKeyOption = @"testKey";
1616
NSString * _Nonnull const BranchJsonConfigUseTestInstanceOption = @"useTestInstance";
17-
NSString * _Nonnull const BranchJsonConfigDelayInitToCheckForSearchAdsOption = @"delayInitToCheckForSearchAds";
18-
NSString * _Nonnull const BranchJsonConfigAppleSearchAdsDebugModeOption = @"appleSearchAdsDebugMode";
19-
NSString * _Nonnull const BranchJsonConfigDeferInitializationForJSLoadOption = @"deferInitializationForJSLoad";
17+
NSString * _Nonnull const BranchJsonConfigDeferInitForPluginRuntimeOption = @"deferInitForPluginRuntime";
18+
NSString * _Nonnull const BranchJsonConfigEnableLogging = @"enableLogging";
2019
NSString * _Nonnull const BranchJsonConfigEnableFacebookLinkCheck = @"enableFacebookLinkCheck";
2120
NSString * _Nonnull const BranchJsonConfigCheckPasteboardOnInstall = @"checkPasteboardOnInstall";
2221

@@ -73,7 +72,7 @@ - (void)loadConfigFile
7372
- (NSData *)configFileContents
7473
{
7574
if (!self.configFileURL) return nil;
76-
BNCLog([NSString stringWithFormat:@"Loading %@", self.configFileURL.pathComponents.lastObject]);
75+
BNCLogDebug([NSString stringWithFormat:@"Loading %@", self.configFileURL.pathComponents.lastObject]);
7776

7877
NSError *error;
7978
NSData *data = [NSData dataWithContentsOfURL:self.configFileURL options:0 error:&error];
@@ -99,12 +98,17 @@ - (void)findConfigFile
9998
@"branch.ios",
10099
@"branch"
101100
];
102-
101+
103102
[filesToCheck enumerateObjectsUsingBlock:^(NSString * _Nonnull file, NSUInteger idx, BOOL * _Nonnull stop) {
104103
configFileURL = [mainBundle URLForResource:file withExtension:@"json"];
105104
*stop = (configFileURL != nil);
106105
}];
107-
106+
107+
// Unity places the config at [[NSBundle mainBundle] bundlePath] + /Data/Raw/branch.json
108+
if (!configFileURL) {
109+
configFileURL = [mainBundle URLForResource:@"branch" withExtension:@"json" subdirectory:@"Data/Raw"];
110+
}
111+
108112
if (!configFileURL) {
109113
BNCLogDebug(@"No branch.json in app bundle.");
110114
return;
@@ -125,21 +129,15 @@ - (BOOL)useTestInstance
125129
return number.boolValue;
126130
}
127131

128-
- (BOOL)delayInitToCheckForSearchAds
129-
{
130-
NSNumber *number = self[BranchJsonConfigDelayInitToCheckForSearchAdsOption];
131-
return number.boolValue;
132-
}
133-
134-
- (BOOL)appleSearchAdsDebugMode
132+
- (BOOL)deferInitForPluginRuntime
135133
{
136-
NSNumber *number = self[BranchJsonConfigAppleSearchAdsDebugModeOption];
134+
NSNumber *number = self[BranchJsonConfigDeferInitForPluginRuntimeOption];
137135
return number.boolValue;
138136
}
139137

140-
- (BOOL)deferInitializationForJSLoad
138+
- (BOOL)enableLogging
141139
{
142-
NSNumber *number = self[BranchJsonConfigDeferInitializationForJSLoadOption];
140+
NSNumber *number = self[BranchJsonConfigEnableLogging];
143141
return number.boolValue;
144142
}
145143

BranchSDK/BranchPluginSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
1313
@interface BranchPluginSupport : NSObject
1414

1515
+ (BranchPluginSupport *)instance;
16+
1617
- (NSDictionary<NSString *, NSString *> *)deviceDescription;
1718

1819
@end

BranchSDK/BranchPluginSupport.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
#import "NSMutableDictionary+Branch.h"
1111
#import "BNCDeviceInfo.h"
1212
#import "BNCPreferenceHelper.h"
13-
14-
@interface BranchPluginSupport()
15-
16-
@end
13+
#import "Branch.h"
1714

1815
@implementation BranchPluginSupport
1916

@@ -26,7 +23,6 @@ + (BranchPluginSupport *)instance {
2623
return pluginSupport;
2724
}
2825

29-
3026
- (NSDictionary<NSString *, NSString *> *)deviceDescription {
3127
NSMutableDictionary<NSString *, NSString *> *dictionary = [NSMutableDictionary new];
3228
BNCDeviceInfo *deviceInfo = [BNCDeviceInfo getInstance];

0 commit comments

Comments
 (0)