1313#import " Branch.h"
1414#import " BranchJsonConfig.h"
1515
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-
2416@interface BranchPluginSupport ()
2517
2618@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;
19+ @property (nonatomic , copy , nullable ) void (^cachedBlock)(void );
3820
3921@end
4022
@@ -52,182 +34,63 @@ + (BranchPluginSupport *)instance {
5234- (instancetype )init {
5335 self = [super init ];
5436 if (self) {
55- // load initial value from branch.json
56- self.deferInitForPlugin = [[BranchJsonConfig instance ] deferInitForPlugin ];
37+ self.deferInitForPlugin = [BranchJsonConfig instance ].deferInitForPlugin ;
5738 }
5839 return self;
5940}
6041
6142- (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- }
43+ [self deferBlock: ^{
44+ [[Branch getInstance ] initSessionWithLaunchOptions: options andRegisterDeepLinkHandler: callback];
8745 }];
8846}
8947
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- }
48+ - (void )initSessionWithLaunchOptions : (nullable NSDictionary *)options registerDeepLinkHandlerUsingBranchUniversalObject : (void (^)(BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error))callback {
49+ [self deferBlock: ^{
50+ [[Branch getInstance ] initSessionWithLaunchOptions: options andRegisterDeepLinkHandlerUsingBranchUniversalObject: callback];
10951 }];
11052}
11153
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-
14054- (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- }
55+ [self deferBlock: ^{
56+ [[Branch getInstance ] handleDeepLink: url sceneIdentifier: nil ];
57+ }];
58+ return YES ;
16059}
16160
16261- (BOOL )continueUserActivity : (nullable NSUserActivity *)userActivity {
163- return [self continueUserActivity: userActivity sceneIdentifier: nil ];
62+ [self deferBlock: ^{
63+ [[Branch getInstance ] continueUserActivity: userActivity sceneIdentifier: nil ];
64+ }];
65+ return YES ;
16466}
16567
166- - (BOOL )continueUserActivity : (nullable NSUserActivity *) userActivity sceneIdentifier : (nullable NSString *) sceneIdentifier {
167- BOOL didCache = NO ;
68+ - (BOOL )deferBlock : ( void (^)( void )) block {
69+ BOOL deferred = NO ;
16870 @synchronized (self) {
16971 if (self.deferInitForPlugin ) {
170- [self clearCachedParams ];
171- self.entry = BNCEntryMethodTypeContinueUserActivity;
172- self.userActivity = userActivity;
173- self.sceneIdentifier = sceneIdentifier;
174- didCache = YES ;
72+ self.cachedBlock = block;
73+ deferred = YES ;
17574 }
17675 }
17776
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 ;
77+ if (!deferred && block) {
78+ block ();
19679 }
80+ return deferred;
19781}
19882
19983- (void )notifyNativeToInit {
20084 @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 ];
22685 self.deferInitForPlugin = NO ;
22786 }
87+
88+ if (self.cachedBlock ) {
89+ self.cachedBlock ();
90+ }
91+ self.cachedBlock = nil ;
22892}
22993
230-
23194- (NSDictionary <NSString *, NSString *> *)deviceDescription {
23295 NSMutableDictionary <NSString *, NSString *> *dictionary = [NSMutableDictionary new ];
23396 BNCDeviceInfo *deviceInfo = [BNCDeviceInfo getInstance ];
0 commit comments