From 6bc64c86da8bf3c86fc12eb13b02375c6bb46a13 Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Tue, 13 Feb 2024 10:42:28 -0800 Subject: [PATCH 1/4] Added method to set API URL --- Branch-TestBed/Branch-TestBed/AppDelegate.m | 5 ++++- BranchSDK/BNCPreferenceHelper.m | 9 ++++----- BranchSDK/BNCServerAPI.m | 7 +++++++ BranchSDK/Branch.h | 6 ++++++ BranchSDK/Branch.m | 4 ++++ BranchSDK/BranchPluginSupport.m | 1 - 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Branch-TestBed/Branch-TestBed/AppDelegate.m b/Branch-TestBed/Branch-TestBed/AppDelegate.m index 09925169f..d59683f7d 100644 --- a/Branch-TestBed/Branch-TestBed/AppDelegate.m +++ b/Branch-TestBed/Branch-TestBed/AppDelegate.m @@ -34,7 +34,10 @@ - (BOOL)application:(UIApplication *)application // Branch.useTestBranchKey = YES; // Make sure to comment this line out for production apps!!! Branch *branch = [Branch getInstance]; - + + // Change the Branch base API URL + //[branch setAPIUrl:@"https://api3.branch.io"]; + // test pre init support //[self testDispatchToIsolationQueue:branch] diff --git a/BranchSDK/BNCPreferenceHelper.m b/BranchSDK/BNCPreferenceHelper.m index fb7a3ca58..dfb9e27cd 100644 --- a/BranchSDK/BNCPreferenceHelper.m +++ b/BranchSDK/BNCPreferenceHelper.m @@ -26,7 +26,7 @@ static NSString * const BRANCH_PREFS_KEY_LAST_RUN_BRANCH_KEY = @"bnc_last_run_branch_key"; static NSString * const BRANCH_PREFS_KEY_LAST_STRONG_MATCH_DATE = @"bnc_strong_match_created_date"; -static NSString * const BRANCH_PREFS_KEY_API_URL = @"bnc_api_url"; +static NSString * const BRANCH_PREFS_KEY_CUSTOM_API_URL = @"bnc_custom_api_url"; static NSString * const BRANCH_PREFS_KEY_PATTERN_LIST_URL = @"bnc_pattern_list_url"; static NSString * const BRANCH_PREFS_KEY_RANDOMIZED_DEVICE_TOKEN = @"bnc_randomized_device_token"; @@ -160,24 +160,23 @@ - (void)setBranchAPIURL:(NSString *)url { if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){ @synchronized (self) { _branchAPIURL = [url copy]; - [self writeObjectToDefaults:BRANCH_PREFS_KEY_API_URL value:_branchAPIURL]; + [self writeObjectToDefaults:BRANCH_PREFS_KEY_CUSTOM_API_URL value:_branchAPIURL]; } } else { BNCLogWarning(@"Ignoring invalid custom API URL"); } } -// TODO: This method is not used with the Tracking domain change. See SDK-2118 - (NSString *)branchAPIURL { @synchronized (self) { if (!_branchAPIURL) { - _branchAPIURL = [self readStringFromDefaults:BRANCH_PREFS_KEY_API_URL]; + _branchAPIURL = [self readStringFromDefaults:BRANCH_PREFS_KEY_CUSTOM_API_URL]; } // return the default URL in the event there's nothing in storage if (_branchAPIURL == nil || [_branchAPIURL isEqualToString:@""]) { _branchAPIURL = [BNC_API_URL copy]; - [self writeObjectToDefaults:BRANCH_PREFS_KEY_API_URL value:_branchAPIURL]; + [self writeObjectToDefaults:BRANCH_PREFS_KEY_CUSTOM_API_URL value:_branchAPIURL]; } return _branchAPIURL; diff --git a/BranchSDK/BNCServerAPI.m b/BranchSDK/BNCServerAPI.m index 2e9bc5c2a..6c7900911 100644 --- a/BranchSDK/BNCServerAPI.m +++ b/BranchSDK/BNCServerAPI.m @@ -9,6 +9,7 @@ #import "BNCSystemObserver.h" #import "BNCConfig.h" #import "BranchConstants.h" +#import "BNCPreferenceHelper.h" @implementation BNCServerAPI @@ -88,6 +89,12 @@ - (NSString *)getBaseURLForLinkingEndpoints { } - (NSString *)getBaseURL { + //Check if user has set a custom API base URL + NSString *url = [[BNCPreferenceHelper sharedInstance] branchAPIURL]; + if (url && ![url isEqualToString:BNC_API_URL]) { + return url; + } + if (self.automaticallyEnableTrackingDomain) { self.useTrackingDomain = [self optedIntoIDFA]; } diff --git a/BranchSDK/Branch.h b/BranchSDK/Branch.h index d4ab71e10..f3701460d 100644 --- a/BranchSDK/Branch.h +++ b/BranchSDK/Branch.h @@ -575,6 +575,12 @@ extern NSString * __nonnull const BNCSpotlightFeature; */ - (void)useEUEndpoints; +/** +Sets a custom base URL for all calls to the Branch API. +@param url Base URL that the Branch API will use. +*/ +- (void)setAPIUrl:(NSString *)url; + /** setDebug is deprecated and all functionality has been disabled. diff --git a/BranchSDK/Branch.m b/BranchSDK/Branch.m index da80d47ee..1d2254a75 100644 --- a/BranchSDK/Branch.m +++ b/BranchSDK/Branch.m @@ -426,6 +426,10 @@ - (void)useEUEndpoints { [BNCServerAPI sharedInstance].useEUServers = YES; } +- (void)setAPIUrl:(NSString *)url { + [[BNCPreferenceHelper sharedInstance] setBranchAPIURL:url]; +} + - (void)setDebug { NSLog(@"Branch setDebug is deprecated and all functionality has been disabled. " "If you wish to enable logging, please invoke enableLogging. " diff --git a/BranchSDK/BranchPluginSupport.m b/BranchSDK/BranchPluginSupport.m index ca07797ed..e577337b3 100644 --- a/BranchSDK/BranchPluginSupport.m +++ b/BranchSDK/BranchPluginSupport.m @@ -54,7 +54,6 @@ + (BranchPluginSupport *)instance { #pragma mark - Server URL methods -// With the change to support Apple's tracking domain feature, this API no longer works. See SDK-2118 // Overrides base API URL + (void)setAPIUrl:(NSString *)url { [[BNCPreferenceHelper sharedInstance] setBranchAPIURL:url]; From 24813678eeb7b0ee4457e4c53d325ed20ee23d43 Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Tue, 13 Feb 2024 14:28:48 -0800 Subject: [PATCH 2/4] Removed method from BranchPluginSupport --- BranchSDK/BranchPluginSupport.h | 6 ------ BranchSDK/BranchPluginSupport.m | 5 ----- 2 files changed, 11 deletions(-) diff --git a/BranchSDK/BranchPluginSupport.h b/BranchSDK/BranchPluginSupport.h index d2f4cfe20..e344dcc8b 100644 --- a/BranchSDK/BranchPluginSupport.h +++ b/BranchSDK/BranchPluginSupport.h @@ -14,12 +14,6 @@ NS_ASSUME_NONNULL_BEGIN + (BranchPluginSupport *)instance; -/** -Sets a custom base URL for all calls to the Branch API. -@param url Base URL that the Branch API will use. -*/ -+ (void)setAPIUrl:(NSString *)url; - /** Sets a custom CDN base URL. @param url Base URL for CDN endpoints. diff --git a/BranchSDK/BranchPluginSupport.m b/BranchSDK/BranchPluginSupport.m index e577337b3..3cf49247b 100644 --- a/BranchSDK/BranchPluginSupport.m +++ b/BranchSDK/BranchPluginSupport.m @@ -54,11 +54,6 @@ + (BranchPluginSupport *)instance { #pragma mark - Server URL methods -// Overrides base API URL -+ (void)setAPIUrl:(NSString *)url { - [[BNCPreferenceHelper sharedInstance] setBranchAPIURL:url]; -} - // Overrides base CDN URL + (void)setCDNBaseUrl:(NSString *)url { [[BNCPreferenceHelper sharedInstance] setPatternListURL:url]; From 9a0722e8df95e73d45e7f4ba590dc19f5f1762eb Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Tue, 13 Feb 2024 17:32:24 -0800 Subject: [PATCH 3/4] Updated to no longer save custom URL --- .../BNCPreferenceHelperTests.m | 28 ------------------ Branch-TestBed/Branch-TestBed/AppDelegate.m | 2 +- BranchSDK/BNCPreferenceHelper.h | 2 -- BranchSDK/BNCPreferenceHelper.m | 29 ------------------- BranchSDK/BNCServerAPI.h | 2 ++ BranchSDK/BNCServerAPI.m | 10 +++++-- BranchSDK/Branch.h | 2 +- BranchSDK/Branch.m | 8 +++-- 8 files changed, 17 insertions(+), 66 deletions(-) diff --git a/Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m b/Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m index 249fbe2de..5269501b3 100644 --- a/Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m +++ b/Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m @@ -199,34 +199,6 @@ - (void)testURLSkipList { XCTAssert([filterDesc isEqualToString:valueDesc]); } -- (void)testSetAPIURL_Example { - - NSString *url = @"https://www.example.com/"; - [self.prefHelper setBranchAPIURL:url] ; - - NSString *urlStored = self.prefHelper.branchAPIURL ; - XCTAssert([url isEqualToString:urlStored]); -} - -- (void)testSetAPIURL_InvalidHttp { - - NSString *url = @"Invalid://www.example.com/"; - [self.prefHelper setBranchAPIURL:url] ; - - NSString *urlStored = self.prefHelper.branchAPIURL ; - XCTAssert(![url isEqualToString:urlStored]); - XCTAssert([urlStored isEqualToString:BNC_API_URL]); -} - -- (void)testSetAPIURL_InvalidEmpty { - - [self.prefHelper setBranchAPIURL:@""] ; - - NSString *urlStored = self.prefHelper.branchAPIURL ; - XCTAssert(![urlStored isEqualToString:@""]); - XCTAssert([urlStored isEqualToString:BNC_API_URL]); -} - - (void)testSetCDNBaseURL_Example { NSString *url = @"https://www.example.com/"; diff --git a/Branch-TestBed/Branch-TestBed/AppDelegate.m b/Branch-TestBed/Branch-TestBed/AppDelegate.m index d59683f7d..d02c3a6cd 100644 --- a/Branch-TestBed/Branch-TestBed/AppDelegate.m +++ b/Branch-TestBed/Branch-TestBed/AppDelegate.m @@ -36,7 +36,7 @@ - (BOOL)application:(UIApplication *)application Branch *branch = [Branch getInstance]; // Change the Branch base API URL - //[branch setAPIUrl:@"https://api3.branch.io"]; + //[Branch setAPIUrl:@"https://api3.branch.io"]; // test pre init support //[self testDispatchToIsolationQueue:branch] diff --git a/BranchSDK/BNCPreferenceHelper.h b/BranchSDK/BNCPreferenceHelper.h index d7f56047b..336012609 100644 --- a/BranchSDK/BNCPreferenceHelper.h +++ b/BranchSDK/BNCPreferenceHelper.h @@ -51,7 +51,6 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void); @property (copy, nonatomic) NSString *lastSystemBuildVersion; @property (copy, nonatomic) NSString *browserUserAgentString; @property (copy, nonatomic) NSString *referringURL; -@property (copy, nonatomic) NSString *branchAPIURL; @property (assign, nonatomic) BOOL limitFacebookTracking; @property (strong, nonatomic) NSDate *previousAppBuildDate; @property (assign, nonatomic, readwrite) BOOL disableAdNetworkCallouts; @@ -77,7 +76,6 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void); + (BNCPreferenceHelper *)sharedInstance; -- (void)setBranchAPIURL:(NSString *)url; - (void)setPatternListURL:(NSString *)url; - (void)setRequestMetadataKey:(NSString *)key value:(NSObject *)value; diff --git a/BranchSDK/BNCPreferenceHelper.m b/BranchSDK/BNCPreferenceHelper.m index dfb9e27cd..486df900a 100644 --- a/BranchSDK/BNCPreferenceHelper.m +++ b/BranchSDK/BNCPreferenceHelper.m @@ -26,7 +26,6 @@ static NSString * const BRANCH_PREFS_KEY_LAST_RUN_BRANCH_KEY = @"bnc_last_run_branch_key"; static NSString * const BRANCH_PREFS_KEY_LAST_STRONG_MATCH_DATE = @"bnc_strong_match_created_date"; -static NSString * const BRANCH_PREFS_KEY_CUSTOM_API_URL = @"bnc_custom_api_url"; static NSString * const BRANCH_PREFS_KEY_PATTERN_LIST_URL = @"bnc_pattern_list_url"; static NSString * const BRANCH_PREFS_KEY_RANDOMIZED_DEVICE_TOKEN = @"bnc_randomized_device_token"; @@ -68,7 +67,6 @@ @interface BNCPreferenceHelper () { NSOperationQueue *_persistPrefsQueue; NSString *_lastSystemBuildVersion; NSString *_browserUserAgentString; - NSString *_branchAPIURL; NSString *_referringURL; } @@ -156,33 +154,6 @@ - (void) dealloc { #pragma mark - API methods -- (void)setBranchAPIURL:(NSString *)url { - if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){ - @synchronized (self) { - _branchAPIURL = [url copy]; - [self writeObjectToDefaults:BRANCH_PREFS_KEY_CUSTOM_API_URL value:_branchAPIURL]; - } - } else { - BNCLogWarning(@"Ignoring invalid custom API URL"); - } -} - -- (NSString *)branchAPIURL { - @synchronized (self) { - if (!_branchAPIURL) { - _branchAPIURL = [self readStringFromDefaults:BRANCH_PREFS_KEY_CUSTOM_API_URL]; - } - - // return the default URL in the event there's nothing in storage - if (_branchAPIURL == nil || [_branchAPIURL isEqualToString:@""]) { - _branchAPIURL = [BNC_API_URL copy]; - [self writeObjectToDefaults:BRANCH_PREFS_KEY_CUSTOM_API_URL value:_branchAPIURL]; - } - - return _branchAPIURL; - } -} - - (void)setPatternListURL:(NSString *)url { if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){ @synchronized (self) { diff --git a/BranchSDK/BNCServerAPI.h b/BranchSDK/BNCServerAPI.h index 6fc218e65..c44c80577 100644 --- a/BranchSDK/BNCServerAPI.h +++ b/BranchSDK/BNCServerAPI.h @@ -33,6 +33,8 @@ NS_ASSUME_NONNULL_BEGIN // Used to enable unit tests without regard for ATT authorization status @property (nonatomic, assign, readwrite) BOOL automaticallyEnableTrackingDomain; +@property (nonatomic, copy, readwrite, nullable) NSString *customAPIURL; + @end NS_ASSUME_NONNULL_END diff --git a/BranchSDK/BNCServerAPI.m b/BranchSDK/BNCServerAPI.m index 6c7900911..7d7e33768 100644 --- a/BranchSDK/BNCServerAPI.m +++ b/BranchSDK/BNCServerAPI.m @@ -30,6 +30,7 @@ - (instancetype)init { self.useTrackingDomain = NO; self.useEUServers = NO; self.automaticallyEnableTrackingDomain = YES; + self.customAPIURL = NULL; } return self; } @@ -78,6 +79,10 @@ - (BOOL)optedIntoIDFA { // Linking endpoints are not used for Ads tracking - (NSString *)getBaseURLForLinkingEndpoints { + if (self.customAPIURL) { + return self.customAPIURL; + } + NSString * urlString; if (self.useEUServers){ urlString = BNC_EU_API_URL; @@ -90,9 +95,8 @@ - (NSString *)getBaseURLForLinkingEndpoints { - (NSString *)getBaseURL { //Check if user has set a custom API base URL - NSString *url = [[BNCPreferenceHelper sharedInstance] branchAPIURL]; - if (url && ![url isEqualToString:BNC_API_URL]) { - return url; + if (self.customAPIURL) { + return self.customAPIURL; } if (self.automaticallyEnableTrackingDomain) { diff --git a/BranchSDK/Branch.h b/BranchSDK/Branch.h index f3701460d..134877928 100644 --- a/BranchSDK/Branch.h +++ b/BranchSDK/Branch.h @@ -579,7 +579,7 @@ extern NSString * __nonnull const BNCSpotlightFeature; Sets a custom base URL for all calls to the Branch API. @param url Base URL that the Branch API will use. */ -- (void)setAPIUrl:(NSString *)url; ++ (void)setAPIUrl:(NSString *)url; /** setDebug is deprecated and all functionality has been disabled. diff --git a/BranchSDK/Branch.m b/BranchSDK/Branch.m index 1d2254a75..feece0a22 100644 --- a/BranchSDK/Branch.m +++ b/BranchSDK/Branch.m @@ -426,8 +426,12 @@ - (void)useEUEndpoints { [BNCServerAPI sharedInstance].useEUServers = YES; } -- (void)setAPIUrl:(NSString *)url { - [[BNCPreferenceHelper sharedInstance] setBranchAPIURL:url]; ++ (void)setAPIUrl:(NSString *)url { + if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){ + [BNCServerAPI sharedInstance].customAPIURL = url; + } else { + BNCLogWarning(@"Ignoring invalid custom API URL"); + } } - (void)setDebug { From 19692a03ef017771cc51c888f5c7567701b6d274 Mon Sep 17 00:00:00 2001 From: nsingh-branch Date: Wed, 14 Feb 2024 17:25:35 -0800 Subject: [PATCH 4/4] Added tests and changed NULL to nil --- .../Branch-SDK-Tests/BNCAPIServerTest.m | 39 +++++++++++++++++++ BranchSDK/BNCServerAPI.m | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Branch-TestBed/Branch-SDK-Tests/BNCAPIServerTest.m b/Branch-TestBed/Branch-SDK-Tests/BNCAPIServerTest.m index 5f70fcb68..b7df6da68 100644 --- a/Branch-TestBed/Branch-SDK-Tests/BNCAPIServerTest.m +++ b/Branch-TestBed/Branch-SDK-Tests/BNCAPIServerTest.m @@ -11,6 +11,7 @@ #import "BNCSystemObserver.h" #import "BNCConfig.h" #import "BranchConstants.h" +#import "Branch.h" @interface BNCAPIServerTest : XCTestCase @@ -370,4 +371,42 @@ - (void)testValidationServiceURL_EUTracking { XCTAssertTrue([url hasPrefix:expectedUrlPrefix]); } +- (void)testDefaultAPIURL { + BNCServerAPI *serverAPI = [BNCServerAPI new]; + XCTAssertNil(serverAPI.customAPIURL); + + NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL]; + NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"]; + XCTAssertEqualObjects(storedUrl, expectedUrl); +} + +- (void)testSetAPIURL_Example { + NSString *url = @"https://www.example.com"; + [Branch setAPIUrl:url]; + + NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL]; + NSString *expectedUrl = [url stringByAppendingString: @"/v1/install"]; + XCTAssertEqualObjects(storedUrl, expectedUrl); + + [Branch setAPIUrl:BNC_API_URL]; +} + +- (void)testSetAPIURL_InvalidHttp { + NSString *url = @"Invalid://www.example.com"; + [Branch setAPIUrl:url]; + + NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL]; + NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"]; + XCTAssertEqualObjects(storedUrl, expectedUrl); +} + +- (void)testSetAPIURL_InvalidEmpty { + NSString *url = @""; + [Branch setAPIUrl:url]; + + NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL]; + NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"]; + XCTAssertEqualObjects(storedUrl, expectedUrl); +} + @end diff --git a/BranchSDK/BNCServerAPI.m b/BranchSDK/BNCServerAPI.m index 7d7e33768..73f8cb253 100644 --- a/BranchSDK/BNCServerAPI.m +++ b/BranchSDK/BNCServerAPI.m @@ -30,7 +30,7 @@ - (instancetype)init { self.useTrackingDomain = NO; self.useEUServers = NO; self.automaticallyEnableTrackingDomain = YES; - self.customAPIURL = NULL; + self.customAPIURL = nil; } return self; }