Skip to content

Commit 9789aec

Browse files
committed
SDK-1831 add cached sdk entry methods
1 parent deed5a8 commit 9789aec

File tree

2 files changed

+215
-10
lines changed

2 files changed

+215
-10
lines changed

BranchSDK/BranchPluginSupport.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,29 @@
77
//
88

99
#import <Foundation/Foundation.h>
10+
#import "BranchUniversalObject.h"
1011

1112
NS_ASSUME_NONNULL_BEGIN
1213

1314
@interface BranchPluginSupport : NSObject
1415

1516
+ (BranchPluginSupport *)instance;
1617

17-
- (NSDictionary<NSString *, NSString *> *)deviceDescription;
18+
#pragma mark - SDK entry points
19+
20+
- (void)initSessionWithLaunchOptions:(nullable NSDictionary *)options registerDeepLinkHandler:(void (^)(NSDictionary * _Nullable params, NSError * _Nullable error))callback;
21+
22+
- (void)initSessionWithLaunchOptions:(nullable NSDictionary *)options registerDeepLinkHandlerUsingBranchUniversalObject:(void (^)(BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error))callback;
23+
24+
- (BOOL)handleDeepLink:(nullable NSURL *)url;
1825

19-
- (BOOL)deferInitForPlugin;
26+
- (BOOL)continueUserActivity:(nullable NSUserActivity *)userActivity;
27+
28+
- (void)notifyNativeToInit;
29+
30+
#pragma mark - Utility methods
31+
32+
- (NSDictionary<NSString *, NSString *> *)deviceDescription;
2033

2134
@end
2235

BranchSDK/BranchPluginSupport.m

Lines changed: 200 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,32 @@
1010
#import "NSMutableDictionary+Branch.h"
1111
#import "BNCDeviceInfo.h"
1212
#import "BNCPreferenceHelper.h"
13+
#import "Branch.h"
1314
#import "BranchJsonConfig.h"
1415

16+
// since most params are nullable, let's explicitly record the entry method
17+
typedef NS_ENUM(NSUInteger, BNCEntryMethodType) {
18+
BNCEntryMethodTypeUnknown,
19+
BNCEntryMethodTypeInitSession,
20+
BNCEntryMethodTypeHandleDeepLink,
21+
BNCEntryMethodTypeContinueUserActivity
22+
};
23+
1524
@interface BranchPluginSupport()
1625

26+
@property (nonatomic, assign, readwrite) BOOL deferInitForPlugin;
27+
28+
// cached entry params
29+
@property (nonatomic, assign, readwrite) BNCEntryMethodType entry;
30+
@property (nonatomic, strong, readwrite) NSDictionary *options;
31+
@property (nonatomic, assign, readwrite) BOOL isReferrable;
32+
@property (nonatomic, assign, readwrite) BOOL explicitlyRequestedReferrable;
33+
@property (nonatomic, assign, readwrite) BOOL automaticallyDisplayController;
34+
@property (nonatomic, copy, nullable) void (^callback)(BNCInitSessionResponse * _Nullable initResponse, NSError * _Nullable error);
35+
@property (nonatomic, copy, readwrite) NSString *sceneIdentifier;
36+
@property (nonatomic, strong, readwrite) NSURL *url;
37+
@property (nonatomic, strong, readwrite) NSUserActivity *userActivity;
38+
1739
@end
1840

1941
@implementation BranchPluginSupport
@@ -27,6 +49,184 @@ + (BranchPluginSupport *)instance {
2749
return pluginSupport;
2850
}
2951

52+
- (instancetype)init {
53+
self = [super init];
54+
if (self) {
55+
// load initial value from branch.json
56+
self.deferInitForPlugin = [[BranchJsonConfig instance] deferInitForPlugin];
57+
}
58+
return self;
59+
}
60+
61+
- (void)initSessionWithLaunchOptions:(nullable NSDictionary *)options registerDeepLinkHandler:(void (^)(NSDictionary * _Nullable params, NSError * _Nullable error))callback {
62+
[self initSessionWithLaunchOptions:options isReferrable:YES explicitlyRequestedReferrable:NO automaticallyDisplayController:NO registerDeepLinkHandler:callback];
63+
}
64+
65+
- (void)initSessionWithLaunchOptions:(nullable NSDictionary *)options registerDeepLinkHandlerUsingBranchUniversalObject:(void (^)(BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error))callback {
66+
[self initSessionWithLaunchOptions:options isReferrable:YES explicitlyRequestedReferrable:NO automaticallyDisplayController:NO registerDeepLinkHandlerUsingBranchUniversalObject:callback];
67+
}
68+
69+
// Maps BNCInitSessionResponse callback to dictionary callback
70+
- (void)initSessionWithLaunchOptions:(NSDictionary *)options
71+
isReferrable:(BOOL)isReferrable
72+
explicitlyRequestedReferrable:(BOOL)explicitlyRequestedReferrable
73+
automaticallyDisplayController:(BOOL)automaticallyDisplayController
74+
registerDeepLinkHandlerUsingBranchUniversalObject:(callbackWithBranchUniversalObject)callback {
75+
[self initSceneSessionWithLaunchOptions:options
76+
isReferrable:isReferrable
77+
explicitlyRequestedReferrable:explicitlyRequestedReferrable
78+
automaticallyDisplayController:automaticallyDisplayController
79+
registerDeepLinkHandler:^(BNCInitSessionResponse * _Nullable initResponse, NSError * _Nullable error) {
80+
if (callback) {
81+
if (initResponse) {
82+
callback(initResponse.universalObject, initResponse.linkProperties, error);
83+
} else {
84+
callback([BranchUniversalObject new], [BranchLinkProperties new], error);
85+
}
86+
}
87+
}];
88+
}
89+
90+
// Maps BNCInitSessionResponse callback to BUO callback
91+
- (void)initSessionWithLaunchOptions:(NSDictionary *)options
92+
isReferrable:(BOOL)isReferrable
93+
explicitlyRequestedReferrable:(BOOL)explicitlyRequestedReferrable
94+
automaticallyDisplayController:(BOOL)automaticallyDisplayController
95+
registerDeepLinkHandler:(callbackWithParams)callback {
96+
97+
[self initSceneSessionWithLaunchOptions:options
98+
isReferrable:isReferrable
99+
explicitlyRequestedReferrable:explicitlyRequestedReferrable
100+
automaticallyDisplayController:automaticallyDisplayController
101+
registerDeepLinkHandler:^(BNCInitSessionResponse * _Nullable initResponse, NSError * _Nullable error) {
102+
if (callback) {
103+
if (initResponse) {
104+
callback(initResponse.params, error);
105+
} else {
106+
callback([NSDictionary new], error);
107+
}
108+
}
109+
}];
110+
}
111+
112+
// initSession with optional caching. Requires the branch.json deferInitForPlugin option
113+
- (void)initSceneSessionWithLaunchOptions:(NSDictionary *)options
114+
isReferrable:(BOOL)isReferrable
115+
explicitlyRequestedReferrable:(BOOL)explicitlyRequestedReferrable
116+
automaticallyDisplayController:(BOOL)automaticallyDisplayController
117+
registerDeepLinkHandler:(void (^)(BNCInitSessionResponse * _Nullable initResponse, NSError * _Nullable error))callback {
118+
BOOL didCache = NO;
119+
@synchronized (self) {
120+
if (self.deferInitForPlugin) {
121+
[self clearCachedParams];
122+
self.entry = BNCEntryMethodTypeInitSession;
123+
self.options = options;
124+
self.isReferrable = isReferrable;
125+
self.explicitlyRequestedReferrable = explicitlyRequestedReferrable;
126+
self.automaticallyDisplayController = automaticallyDisplayController;
127+
self.callback = callback;
128+
}
129+
}
130+
131+
if (!didCache) {
132+
[[Branch getInstance] initSceneSessionWithLaunchOptions:options
133+
isReferrable:isReferrable
134+
explicitlyRequestedReferrable:explicitlyRequestedReferrable
135+
automaticallyDisplayController:automaticallyDisplayController
136+
registerDeepLinkHandler:callback];
137+
}
138+
}
139+
140+
- (BOOL)handleDeepLink:(nullable NSURL *)url {
141+
return [self handleDeepLink:url sceneIdentifier:nil];
142+
}
143+
144+
- (BOOL)handleDeepLink:(nullable NSURL *)url sceneIdentifier:(nullable NSString *)sceneIdentifier {
145+
BOOL didCache = NO;
146+
@synchronized (self) {
147+
if (self.deferInitForPlugin) {
148+
[self clearCachedParams];
149+
self.entry = BNCEntryMethodTypeHandleDeepLink;
150+
self.url = url;
151+
self.sceneIdentifier = sceneIdentifier;
152+
}
153+
}
154+
155+
if (didCache) {
156+
return NO;
157+
} else {
158+
return [[Branch getInstance] handleDeepLink:url sceneIdentifier:sceneIdentifier];
159+
}
160+
}
161+
162+
- (BOOL)continueUserActivity:(nullable NSUserActivity *)userActivity {
163+
return [self continueUserActivity:userActivity sceneIdentifier:nil];
164+
}
165+
166+
- (BOOL)continueUserActivity:(nullable NSUserActivity *)userActivity sceneIdentifier:(nullable NSString *)sceneIdentifier {
167+
BOOL didCache = NO;
168+
@synchronized (self) {
169+
if (self.deferInitForPlugin) {
170+
[self clearCachedParams];
171+
self.entry = BNCEntryMethodTypeContinueUserActivity;
172+
self.userActivity = userActivity;
173+
self.sceneIdentifier = sceneIdentifier;
174+
didCache = YES;
175+
}
176+
}
177+
178+
if (didCache) {
179+
return NO;
180+
} else {
181+
return [[Branch getInstance] continueUserActivity:userActivity sceneIdentifier:sceneIdentifier];
182+
}
183+
}
184+
185+
- (void)clearCachedParams {
186+
@synchronized (self) {
187+
self.entry = BNCEntryMethodTypeUnknown;
188+
self.options = nil;
189+
self.isReferrable = NO;
190+
self.explicitlyRequestedReferrable = NO;
191+
self.automaticallyDisplayController = NO;
192+
self.callback = nil;
193+
self.sceneIdentifier = nil;
194+
self.url = nil;
195+
self.userActivity = nil;
196+
}
197+
}
198+
199+
- (void)notifyNativeToInit {
200+
@synchronized (self) {
201+
// call cached entry method
202+
switch (self.entry) {
203+
case BNCEntryMethodTypeInitSession:
204+
[[Branch getInstance] initSceneSessionWithLaunchOptions:self.options
205+
isReferrable:self.isReferrable
206+
explicitlyRequestedReferrable:self.explicitlyRequestedReferrable
207+
automaticallyDisplayController:self.automaticallyDisplayController
208+
registerDeepLinkHandler:self.callback];
209+
break;
210+
211+
case BNCEntryMethodTypeHandleDeepLink:
212+
[[Branch getInstance] handleDeepLink:self.url sceneIdentifier:self.sceneIdentifier];
213+
break;
214+
215+
case BNCEntryMethodTypeContinueUserActivity:
216+
[[Branch getInstance] continueUserActivity:self.userActivity sceneIdentifier:self.sceneIdentifier];
217+
break;
218+
219+
case BNCEntryMethodTypeUnknown:
220+
break;
221+
default:
222+
break;
223+
}
224+
225+
[self clearCachedParams];
226+
self.deferInitForPlugin = NO;
227+
}
228+
}
229+
30230

31231
- (NSDictionary<NSString *, NSString *> *)deviceDescription {
32232
NSMutableDictionary<NSString *, NSString *> *dictionary = [NSMutableDictionary new];
@@ -54,12 +254,4 @@ + (BranchPluginSupport *)instance {
54254
return dictionary;
55255
}
56256

57-
- (BOOL)deferInitForPlugin {
58-
BranchJsonConfig *config = BranchJsonConfig.instance;
59-
if (config.deferInitForPlugin) {
60-
return YES;
61-
}
62-
return NO;
63-
}
64-
65257
@end

0 commit comments

Comments
 (0)