Skip to content

Commit 73f64f8

Browse files
authored
Merge pull request #405 from BranchMetrics/refactoring
Add support for setting keys from JS
2 parents e4f2fe8 + bdbb50c commit 73f64f8

File tree

14 files changed

+92
-30
lines changed

14 files changed

+92
-30
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["react-native"]
3-
}
2+
"presets": ["module:metro-react-native-babel-preset"]
3+
}

examples/testbed_native_ios/testbed_native_ios.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@
241241
);
242242
runOnlyForDeploymentPostprocessing = 0;
243243
shellPath = /bin/sh;
244-
shellScript = "export RCT_METRO_PORT=\"${RCT_METRO_PORT:=8081}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/../scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\nif nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\nif ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\necho \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\nexit 2\nfi\nelse\nopen \"$SRCROOT/../scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\nfi\nfi\n";
244+
shellScript = "export RCT_METRO_PORT=\"${RCT_METRO_PORT:=8081}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/node_modules/react-native/scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\nif nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\nif ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\necho \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\nexit 2\nfi\nelse\nopen \"$SRCROOT/node_modules/react-native/scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\nfi\nfi\n";
245245
showEnvVarsInLog = 0;
246246
};
247247
/* End PBXShellScriptBuildPhase section */
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"debugMode": true,
33
"delayInitToCheckForSearchAds": true,
4-
"appleSearchAdsDebugMode": true
4+
"appleSearchAdsDebugMode": true,
5+
"deferInitializationForJSLoad": true
56
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"delayInitToCheckForSearchAds": true
2+
"delayInitToCheckForSearchAds": true,
3+
"deferInitializationForJSLoad": true
34
}

examples/webview_example/src/ArticleList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ArticleList extends Component {
3535
}
3636

3737
componentDidMount() {
38+
// branch.key = 'key_test_jmzd0lUR9gIiAAEQfo42onbcExe1MeHg'
3839
this._unsubscribeFromBranch = branch.subscribe(({ error, params }) => {
3940
if (error) {
4041
console.error("Error from Branch: " + error)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["react-native"]
3-
}
2+
"presets": ["module:metro-react-native-babel-preset"]
3+
}

examples/webview_example_native_ios/WebViewExample.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
);
226226
runOnlyForDeploymentPostprocessing = 0;
227227
shellPath = /bin/sh;
228-
shellScript = "export RCT_METRO_PORT=\"${RCT_METRO_PORT:=8081}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/../scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\nif nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\nif ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\necho \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\nexit 2\nfi\nelse\nopen \"$SRCROOT/../scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\nfi\nfi\n";
228+
shellScript = "export RCT_METRO_PORT=\"${RCT_METRO_PORT:=8081}\"\necho \"export RCT_METRO_PORT=${RCT_METRO_PORT}\" > \"${SRCROOT}/node_modules/react-native/scripts/.packager.env\"\nif [ -z \"${RCT_NO_LAUNCH_PACKAGER+xxx}\" ] ; then\nif nc -w 5 -z localhost ${RCT_METRO_PORT} ; then\nif ! curl -s \"http://localhost:${RCT_METRO_PORT}/status\" | grep -q \"packager-status:running\" ; then\necho \"Port ${RCT_METRO_PORT} already in use, packager is either not running or not running correctly\"\nexit 2\nfi\nelse\nopen \"$SRCROOT/node_modules/react-native/scripts/launchPackager.command\" || echo \"Can't start packager automatically\"\nfi\nfi\n";
229229
showEnvVarsInLog = 0;
230230
};
231231
7B5C791C1E959348002FE942 /* Bundle React Native code and images */ = {

examples/webview_example_native_ios/WebViewExample/AppDelegate.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import UIKit
10-
import react_native_branch
10+
import Branch
1111

1212
@UIApplicationMain
1313
class AppDelegate: UIResponder, UIApplicationDelegate {
@@ -26,29 +26,38 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2626
ReactBridge.start(launchOptions: launchOptions)
2727

2828
// Initialize Branch SDK
29-
NotificationCenter.default.addObserver(self, selector: #selector(routeURLFromBranch), name: NSNotification.Name.RNBranchLinkOpened, object: nil)
3029

3130
#if USE_BRANCH_TEST_INSTANCE
32-
RNBranch.useTestInstance()
31+
Branch.setUseTestBranchKey(true)
3332
#endif
34-
RNBranch.initSession(launchOptions: launchOptions, isReferrable: true)
33+
Branch.getInstance()?.initSession(launchOptions: launchOptions) {
34+
branchUniversalObject, linkProperties, error in
35+
36+
guard error == nil else {
37+
print("Error from Branch: \(error!)")
38+
return
39+
}
40+
41+
guard let branchUniversalObject = branchUniversalObject else { return }
42+
43+
self.routeURLFromBranch(branchUniversalObject: branchUniversalObject)
44+
}
3545

3646
return true
3747
}
3848

3949
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
40-
return RNBranch.branch.application(app, open: url, options: options)
50+
return Branch.getInstance().application(app, open: url, options: options)
4151
}
4252

4353
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
44-
return RNBranch.continue(userActivity)
54+
return Branch.getInstance().continue(userActivity)
4555
}
4656

4757
// MARK: - Branch link routing
4858

49-
@objc func routeURLFromBranch(_ notification: NSNotification) {
50-
guard let buo = notification.userInfo?[RNBranchLinkOpenedNotificationBranchUniversalObjectKey] as? BranchUniversalObject,
51-
let planetData = PlanetData(branchUniversalObject: buo) else { return }
59+
@objc func routeURLFromBranch(branchUniversalObject: BranchUniversalObject) {
60+
guard let planetData = PlanetData(branchUniversalObject: branchUniversalObject) else { return }
5261

5362
let articleViewController = ArticleViewController(planetData: planetData)
5463
navigationController.pushViewController(articleViewController, animated: true)

ios/RNBranch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern NSString * _Nonnull const RNBranchLinkOpenedNotificationLinkPropertiesKey
2424

2525
// Must be called before any other static method below
2626
+ (void)useTestInstance;
27+
+ (void)deferInitializationForJSLoad;
2728

2829
+ (void)setDebug;
2930
+ (void)delayInitToCheckForSearchAds;

ios/RNBranch.m

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#import "RNBranch.h"
2-
#import <React/RCTBridge.h>
32
#import <React/RCTEventDispatcher.h>
43
#import <React/RCTLog.h>
54
#import "BranchEvent+RNBranch.h"
@@ -18,14 +17,17 @@
1817

1918
static NSDictionary *initSessionWithLaunchOptionsResult;
2019
static BOOL useTestInstance = NO;
20+
static NSDictionary *savedLaunchOptions;
21+
static BOOL savedIsReferrable;
22+
static NSString *branchKey;
23+
static BOOL deferInitializationForJSLoad = NO;
2124

2225
static NSString * const IdentFieldName = @"ident";
2326

2427
// These are only really exposed to the JS layer, so keep them internal for now.
2528
static NSString * const RNBranchErrorDomain = @"RNBranchErrorDomain";
2629
static NSInteger const RNBranchUniversalObjectNotFoundError = 1;
2730

28-
2931
#pragma mark - Private RNBranch declarations
3032

3133
@interface RNBranch()
@@ -37,8 +39,6 @@ @interface RNBranch()
3739

3840
@implementation RNBranch
3941

40-
@synthesize bridge = _bridge;
41-
4242
RCT_EXPORT_MODULE();
4343

4444
+ (Branch *)branch
@@ -51,7 +51,7 @@ + (Branch *)branch
5151

5252
// YES if either [RNBranch useTestInstance] was called or useTestInstance: true is present in branch.json.
5353
BOOL usingTestInstance = useTestInstance || config.useTestInstance;
54-
NSString *key = config.branchKey ?: usingTestInstance ? config.testKey : config.liveKey;
54+
NSString *key = branchKey ?: config.branchKey ?: usingTestInstance ? config.testKey : config.liveKey;
5555

5656
if (key) {
5757
// Override the Info.plist if these are present.
@@ -153,23 +153,36 @@ + (void)setRequestMetadataKey:(NSString *)key value:(NSObject *)value
153153
+ (void)useTestInstance {
154154
useTestInstance = YES;
155155
}
156+
157+
+ (void)deferInitializationForJSLoad
158+
{
159+
deferInitializationForJSLoad = YES;
160+
}
156161

157162
//Called by AppDelegate.m -- stores initSession result in static variables and posts RNBranchLinkOpened event that's captured by the RNBranch instance to emit it to React Native
158163
+ (void)initSessionWithLaunchOptions:(NSDictionary *)launchOptions isReferrable:(BOOL)isReferrable {
159-
[self.branch initSessionWithLaunchOptions:launchOptions isReferrable:isReferrable andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {
164+
savedLaunchOptions = launchOptions;
165+
savedIsReferrable = isReferrable;
166+
167+
if (!deferInitializationForJSLoad && !RNBranchConfig.instance.deferInitializationForJSLoad) [self initializeBranchSDK];
168+
}
169+
170+
+ (void)initializeBranchSDK
171+
{
172+
[self.branch initSessionWithLaunchOptions:savedLaunchOptions isReferrable:savedIsReferrable andRegisterDeepLinkHandler:^(NSDictionary *params, NSError *error) {
160173
NSMutableDictionary *result = [NSMutableDictionary dictionary];
161174
if (error) result[RNBranchLinkOpenedNotificationErrorKey] = error;
162175
if (params) {
163176
result[RNBranchLinkOpenedNotificationParamsKey] = params;
164177
BOOL clickedBranchLink = params[@"+clicked_branch_link"];
165-
178+
166179
if (clickedBranchLink) {
167180
BranchUniversalObject *branchUniversalObject = [BranchUniversalObject objectWithDictionary:params];
168181
if (branchUniversalObject) result[RNBranchLinkOpenedNotificationBranchUniversalObjectKey] = branchUniversalObject;
169-
182+
170183
BranchLinkProperties *linkProperties = [BranchLinkProperties getBranchLinkPropertiesFromDictionary:params];
171184
if (linkProperties) result[RNBranchLinkOpenedNotificationLinkPropertiesKey] = linkProperties;
172-
185+
173186
if (params[@"~referring_link"]) {
174187
result[RNBranchLinkOpenedNotificationUriKey] = [NSURL URLWithString:params[@"~referring_link"]];
175188
}
@@ -178,7 +191,7 @@ + (void)initSessionWithLaunchOptions:(NSDictionary *)launchOptions isReferrable:
178191
result[RNBranchLinkOpenedNotificationUriKey] = [NSURL URLWithString:params[@"+non_branch_link"]];
179192
}
180193
}
181-
194+
182195
[[NSNotificationCenter defaultCenter] postNotificationName:RNBranchLinkOpenedNotification object:nil userInfo:result];
183196
}];
184197
}
@@ -291,6 +304,24 @@ - (BranchUniversalObject *)findUniversalObjectWithIdent:(NSString *)ident reject
291304
resolve([Branch trackingDisabled] ? @YES : @NO);
292305
}
293306

307+
#pragma mark initializeBranch
308+
RCT_EXPORT_METHOD(initializeBranch:(NSString *)key
309+
resolver:(RCTPromiseResolveBlock)resolve
310+
rejecter:(__unused RCTPromiseRejectBlock)reject
311+
) {
312+
if (!deferInitializationForJSLoad && !RNBranchConfig.instance.deferInitializationForJSLoad) {
313+
// This is a no-op from JS unless [RNBranch deferInitializationForJSLoad] is called.
314+
resolve(0);
315+
return;
316+
}
317+
318+
RCTLogTrace(@"Initializing Branch SDK. Key from JS: %@", key);
319+
branchKey = key;
320+
321+
[self.class initializeBranchSDK];
322+
resolve(0);
323+
}
324+
294325
#pragma mark redeemInitSessionResult
295326
RCT_EXPORT_METHOD(
296327
redeemInitSessionResult:(RCTPromiseResolveBlock)resolve

0 commit comments

Comments
 (0)