Skip to content

Commit 6a43f49

Browse files
authored
Merge pull request #1350 from BranchMetrics/SDK-2118
[SDK-2118] Added a method to set API URL
2 parents 8eb3280 + 19692a0 commit 6a43f49

File tree

11 files changed

+70
-73
lines changed

11 files changed

+70
-73
lines changed

Branch-TestBed/Branch-SDK-Tests/BNCAPIServerTest.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "BNCSystemObserver.h"
1212
#import "BNCConfig.h"
1313
#import "BranchConstants.h"
14+
#import "Branch.h"
1415

1516
@interface BNCAPIServerTest : XCTestCase
1617

@@ -370,4 +371,42 @@ - (void)testValidationServiceURL_EUTracking {
370371
XCTAssertTrue([url hasPrefix:expectedUrlPrefix]);
371372
}
372373

374+
- (void)testDefaultAPIURL {
375+
BNCServerAPI *serverAPI = [BNCServerAPI new];
376+
XCTAssertNil(serverAPI.customAPIURL);
377+
378+
NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
379+
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
380+
XCTAssertEqualObjects(storedUrl, expectedUrl);
381+
}
382+
383+
- (void)testSetAPIURL_Example {
384+
NSString *url = @"https://www.example.com";
385+
[Branch setAPIUrl:url];
386+
387+
NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
388+
NSString *expectedUrl = [url stringByAppendingString: @"/v1/install"];
389+
XCTAssertEqualObjects(storedUrl, expectedUrl);
390+
391+
[Branch setAPIUrl:BNC_API_URL];
392+
}
393+
394+
- (void)testSetAPIURL_InvalidHttp {
395+
NSString *url = @"Invalid://www.example.com";
396+
[Branch setAPIUrl:url];
397+
398+
NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
399+
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
400+
XCTAssertEqualObjects(storedUrl, expectedUrl);
401+
}
402+
403+
- (void)testSetAPIURL_InvalidEmpty {
404+
NSString *url = @"";
405+
[Branch setAPIUrl:url];
406+
407+
NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
408+
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
409+
XCTAssertEqualObjects(storedUrl, expectedUrl);
410+
}
411+
373412
@end

Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,34 +199,6 @@ - (void)testURLSkipList {
199199
XCTAssert([filterDesc isEqualToString:valueDesc]);
200200
}
201201

202-
- (void)testSetAPIURL_Example {
203-
204-
NSString *url = @"https://www.example.com/";
205-
[self.prefHelper setBranchAPIURL:url] ;
206-
207-
NSString *urlStored = self.prefHelper.branchAPIURL ;
208-
XCTAssert([url isEqualToString:urlStored]);
209-
}
210-
211-
- (void)testSetAPIURL_InvalidHttp {
212-
213-
NSString *url = @"Invalid://www.example.com/";
214-
[self.prefHelper setBranchAPIURL:url] ;
215-
216-
NSString *urlStored = self.prefHelper.branchAPIURL ;
217-
XCTAssert(![url isEqualToString:urlStored]);
218-
XCTAssert([urlStored isEqualToString:BNC_API_URL]);
219-
}
220-
221-
- (void)testSetAPIURL_InvalidEmpty {
222-
223-
[self.prefHelper setBranchAPIURL:@""] ;
224-
225-
NSString *urlStored = self.prefHelper.branchAPIURL ;
226-
XCTAssert(![urlStored isEqualToString:@""]);
227-
XCTAssert([urlStored isEqualToString:BNC_API_URL]);
228-
}
229-
230202
- (void)testSetCDNBaseURL_Example {
231203

232204
NSString *url = @"https://www.example.com/";

Branch-TestBed/Branch-TestBed/AppDelegate.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ - (BOOL)application:(UIApplication *)application
3434

3535
// Branch.useTestBranchKey = YES; // Make sure to comment this line out for production apps!!!
3636
Branch *branch = [Branch getInstance];
37-
37+
38+
// Change the Branch base API URL
39+
//[Branch setAPIUrl:@"https://api3.branch.io"];
40+
3841
// test pre init support
3942
//[self testDispatchToIsolationQueue:branch]
4043

BranchSDK/BNCPreferenceHelper.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void);
5151
@property (copy, nonatomic) NSString *lastSystemBuildVersion;
5252
@property (copy, nonatomic) NSString *browserUserAgentString;
5353
@property (copy, nonatomic) NSString *referringURL;
54-
@property (copy, nonatomic) NSString *branchAPIURL;
5554
@property (assign, nonatomic) BOOL limitFacebookTracking;
5655
@property (strong, nonatomic) NSDate *previousAppBuildDate;
5756
@property (assign, nonatomic, readwrite) BOOL disableAdNetworkCallouts;
@@ -81,7 +80,6 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void);
8180

8281
+ (BNCPreferenceHelper *)sharedInstance;
8382

84-
- (void)setBranchAPIURL:(NSString *)url;
8583
- (void)setPatternListURL:(NSString *)url;
8684

8785
- (void)setRequestMetadataKey:(NSString *)key value:(NSObject *)value;

BranchSDK/BNCPreferenceHelper.m

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
static NSString * const BRANCH_PREFS_KEY_LAST_RUN_BRANCH_KEY = @"bnc_last_run_branch_key";
2727
static NSString * const BRANCH_PREFS_KEY_LAST_STRONG_MATCH_DATE = @"bnc_strong_match_created_date";
2828

29-
static NSString * const BRANCH_PREFS_KEY_API_URL = @"bnc_api_url";
3029
static NSString * const BRANCH_PREFS_KEY_PATTERN_LIST_URL = @"bnc_pattern_list_url";
3130

3231
static NSString * const BRANCH_PREFS_KEY_RANDOMIZED_DEVICE_TOKEN = @"bnc_randomized_device_token";
@@ -72,7 +71,6 @@ @interface BNCPreferenceHelper () {
7271
NSOperationQueue *_persistPrefsQueue;
7372
NSString *_lastSystemBuildVersion;
7473
NSString *_browserUserAgentString;
75-
NSString *_branchAPIURL;
7674
NSString *_referringURL;
7775
}
7876

@@ -163,34 +161,6 @@ - (void) dealloc {
163161

164162
#pragma mark - API methods
165163

166-
- (void)setBranchAPIURL:(NSString *)url {
167-
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){
168-
@synchronized (self) {
169-
_branchAPIURL = [url copy];
170-
[self writeObjectToDefaults:BRANCH_PREFS_KEY_API_URL value:_branchAPIURL];
171-
}
172-
} else {
173-
BNCLogWarning(@"Ignoring invalid custom API URL");
174-
}
175-
}
176-
177-
// TODO: This method is not used with the Tracking domain change. See SDK-2118
178-
- (NSString *)branchAPIURL {
179-
@synchronized (self) {
180-
if (!_branchAPIURL) {
181-
_branchAPIURL = [self readStringFromDefaults:BRANCH_PREFS_KEY_API_URL];
182-
}
183-
184-
// return the default URL in the event there's nothing in storage
185-
if (_branchAPIURL == nil || [_branchAPIURL isEqualToString:@""]) {
186-
_branchAPIURL = [BNC_API_URL copy];
187-
[self writeObjectToDefaults:BRANCH_PREFS_KEY_API_URL value:_branchAPIURL];
188-
}
189-
190-
return _branchAPIURL;
191-
}
192-
}
193-
194164
- (void)setPatternListURL:(NSString *)url {
195165
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){
196166
@synchronized (self) {

BranchSDK/BNCServerAPI.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ NS_ASSUME_NONNULL_BEGIN
3333
// Used to enable unit tests without regard for ATT authorization status
3434
@property (nonatomic, assign, readwrite) BOOL automaticallyEnableTrackingDomain;
3535

36+
@property (nonatomic, copy, readwrite, nullable) NSString *customAPIURL;
37+
3638
@end
3739

3840
NS_ASSUME_NONNULL_END

BranchSDK/BNCServerAPI.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "BNCSystemObserver.h"
1010
#import "BNCConfig.h"
1111
#import "BranchConstants.h"
12+
#import "BNCPreferenceHelper.h"
1213

1314
@implementation BNCServerAPI
1415

@@ -29,6 +30,7 @@ - (instancetype)init {
2930
self.useTrackingDomain = NO;
3031
self.useEUServers = NO;
3132
self.automaticallyEnableTrackingDomain = YES;
33+
self.customAPIURL = nil;
3234
}
3335
return self;
3436
}
@@ -77,6 +79,10 @@ - (BOOL)optedIntoIDFA {
7779

7880
// Linking endpoints are not used for Ads tracking
7981
- (NSString *)getBaseURLForLinkingEndpoints {
82+
if (self.customAPIURL) {
83+
return self.customAPIURL;
84+
}
85+
8086
NSString * urlString;
8187
if (self.useEUServers){
8288
urlString = BNC_EU_API_URL;
@@ -88,6 +94,11 @@ - (NSString *)getBaseURLForLinkingEndpoints {
8894
}
8995

9096
- (NSString *)getBaseURL {
97+
//Check if user has set a custom API base URL
98+
if (self.customAPIURL) {
99+
return self.customAPIURL;
100+
}
101+
91102
if (self.automaticallyEnableTrackingDomain) {
92103
self.useTrackingDomain = [self optedIntoIDFA];
93104
}

BranchSDK/Branch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,12 @@ extern NSString * __nonnull const BNCSpotlightFeature;
575575
*/
576576
- (void)useEUEndpoints;
577577

578+
/**
579+
Sets a custom base URL for all calls to the Branch API.
580+
@param url Base URL that the Branch API will use.
581+
*/
582+
+ (void)setAPIUrl:(NSString *)url;
583+
578584
/**
579585
setDebug is deprecated and all functionality has been disabled.
580586

BranchSDK/Branch.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,14 @@ - (void)useEUEndpoints {
426426
[BNCServerAPI sharedInstance].useEUServers = YES;
427427
}
428428

429+
+ (void)setAPIUrl:(NSString *)url {
430+
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){
431+
[BNCServerAPI sharedInstance].customAPIURL = url;
432+
} else {
433+
BNCLogWarning(@"Ignoring invalid custom API URL");
434+
}
435+
}
436+
429437
- (void)setDebug {
430438
NSLog(@"Branch setDebug is deprecated and all functionality has been disabled. "
431439
"If you wish to enable logging, please invoke enableLogging. "

BranchSDK/BranchPluginSupport.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ NS_ASSUME_NONNULL_BEGIN
1414

1515
+ (BranchPluginSupport *)instance;
1616

17-
/**
18-
Sets a custom base URL for all calls to the Branch API.
19-
@param url Base URL that the Branch API will use.
20-
*/
21-
+ (void)setAPIUrl:(NSString *)url;
22-
2317
/**
2418
Sets a custom CDN base URL.
2519
@param url Base URL for CDN endpoints.

0 commit comments

Comments
 (0)