1 | //
|
2 | // RNBranchConfig.m
|
3 | // Pods
|
4 | //
|
5 | // Created by Jimmy Dee on 6/7/17.
|
6 | //
|
7 | //
|
8 |
|
9 | #import <React/RCTLog.h>
|
10 |
|
11 | #import "RNBranchConfig.h"
|
12 |
|
13 | NSString * _Nonnull const RNBranchConfigDebugModeOption = @"debugMode";
|
14 | NSString * _Nonnull const RNBranchConfigBranchKeyOption = @"branchKey";
|
15 | NSString * _Nonnull const RNBranchConfigLiveKeyOption = @"liveKey";
|
16 | NSString * _Nonnull const RNBranchConfigTestKeyOption = @"testKey";
|
17 | NSString * _Nonnull const RNBranchConfigUseTestInstanceOption = @"useTestInstance";
|
18 | NSString * _Nonnull const RNBranchConfigDelayInitToCheckForSearchAdsOption = @"delayInitToCheckForSearchAds";
|
19 | NSString * _Nonnull const RNBranchConfigAppleSearchAdsDebugModeOption = @"appleSearchAdsDebugMode";
|
20 | NSString * _Nonnull const RNBranchConfigDeferInitializationForJSLoadOption = @"deferInitializationForJSLoad";
|
21 |
|
22 | @interface RNBranchConfig()
|
23 | @property (nonatomic) NSDictionary *configuration;
|
24 | @property (nonatomic, readonly) NSData *configFileContents;
|
25 | @property (nonatomic) NSURL *configFileURL;
|
26 | @end
|
27 |
|
28 | @implementation RNBranchConfig
|
29 |
|
30 | + (RNBranchConfig * _Nonnull)instance
|
31 | {
|
32 | @synchronized(self) {
|
33 | static RNBranchConfig *_instance;
|
34 | static dispatch_once_t once = 0;
|
35 | dispatch_once(&once, ^{
|
36 | _instance = [[RNBranchConfig alloc] init];
|
37 | });
|
38 | return _instance;
|
39 | }
|
40 | }
|
41 |
|
42 | - (instancetype)init
|
43 | {
|
44 | self = [super init];
|
45 | if (self) {
|
46 | [self findConfigFile];
|
47 | [self loadConfigFile];
|
48 | }
|
49 | return self;
|
50 | }
|
51 |
|
52 | - (void)loadConfigFile
|
53 | {
|
54 | NSData *data = self.configFileContents;
|
55 | if (!data) return;
|
56 |
|
57 | NSError *error;
|
58 | id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
59 | if (!object || error) {
|
60 | RCTLogError(@"Failed to parse branch.json. Error: %@", error.localizedDescription);
|
61 | return;
|
62 | }
|
63 |
|
64 | if (![object isKindOfClass:NSDictionary.class]) {
|
65 | RCTLogError(@"Contents of branch.json should be a JSON object.");
|
66 | return;
|
67 | }
|
68 |
|
69 | self.configuration = object;
|
70 | }
|
71 |
|
72 | - (NSData *)configFileContents
|
73 | {
|
74 | if (!self.configFileURL) return nil;
|
75 | RCTLogInfo(@"Loading %@", self.configFileURL.pathComponents.lastObject);
|
76 |
|
77 | NSError *error;
|
78 | NSData *data = [NSData dataWithContentsOfURL:self.configFileURL options:0 error:&error];
|
79 | if (!data || error) {
|
80 | RCTLogError(@"Failed to load %@. Error: %@", self.configFileURL, error.localizedDescription);
|
81 | return nil;
|
82 | }
|
83 | return data;
|
84 | }
|
85 |
|
86 | - (void)findConfigFile
|
87 | {
|
88 | if (self.configFileURL) return;
|
89 |
|
90 | __block NSURL *configFileURL;
|
91 | NSBundle *mainBundle = NSBundle.mainBundle;
|
92 | NSArray *filesToCheck =
|
93 | @[
|
94 | #ifdef DEBUG
|
95 | @"branch.ios.debug",
|
96 | @"branch.debug",
|
97 | #endif // DEBUG
|
98 | @"branch.ios",
|
99 | @"branch"
|
100 | ];
|
101 |
|
102 | [filesToCheck enumerateObjectsUsingBlock:^(NSString * _Nonnull file, NSUInteger idx, BOOL * _Nonnull stop) {
|
103 | configFileURL = [mainBundle URLForResource:file withExtension:@"json"];
|
104 | *stop = (configFileURL != nil);
|
105 | }];
|
106 |
|
107 | if (!configFileURL) {
|
108 | RCTLogInfo(@"Could not find branch.json in app bundle.");
|
109 | return;
|
110 | }
|
111 |
|
112 | self.configFileURL = configFileURL;
|
113 | }
|
114 |
|
115 | - (BOOL)debugMode
|
116 | {
|
117 | NSNumber *number = self[RNBranchConfigDebugModeOption];
|
118 | return number.boolValue;
|
119 | }
|
120 |
|
121 | - (BOOL)useTestInstance
|
122 | {
|
123 | NSNumber *number = self[RNBranchConfigUseTestInstanceOption];
|
124 | return number.boolValue;
|
125 | }
|
126 |
|
127 | - (BOOL)delayInitToCheckForSearchAds
|
128 | {
|
129 | NSNumber *number = self[RNBranchConfigDelayInitToCheckForSearchAdsOption];
|
130 | return number.boolValue;
|
131 | }
|
132 |
|
133 | - (BOOL)appleSearchAdsDebugMode
|
134 | {
|
135 | NSNumber *number = self[RNBranchConfigAppleSearchAdsDebugModeOption];
|
136 | return number.boolValue;
|
137 | }
|
138 |
|
139 | - (BOOL)deferInitializationForJSLoad
|
140 | {
|
141 | NSNumber *number = self[RNBranchConfigDeferInitializationForJSLoadOption];
|
142 | return number.boolValue;
|
143 | }
|
144 |
|
145 | - (NSString *)branchKey
|
146 | {
|
147 | return self[RNBranchConfigBranchKeyOption];
|
148 | }
|
149 |
|
150 | - (NSString *)liveKey
|
151 | {
|
152 | return self[RNBranchConfigLiveKeyOption];
|
153 | }
|
154 |
|
155 | - (NSString *)testKey
|
156 | {
|
157 | return self[RNBranchConfigTestKeyOption];
|
158 | }
|
159 |
|
160 | - (id)objectForKey:(NSString *)key
|
161 | {
|
162 | return self.configuration[key];
|
163 | }
|
164 |
|
165 | - (id)objectForKeyedSubscript:(NSString *)key
|
166 | {
|
167 | return self.configuration[key];
|
168 | }
|
169 |
|
170 | @end
|