diff --git a/Branch-TestBed/Branch-SDK-Tests/BNCClassSerializationTests.m b/Branch-TestBed/Branch-SDK-Tests/BNCClassSerializationTests.m index f1af287d9..d6293a4b3 100644 --- a/Branch-TestBed/Branch-SDK-Tests/BNCClassSerializationTests.m +++ b/Branch-TestBed/Branch-SDK-Tests/BNCClassSerializationTests.m @@ -71,7 +71,8 @@ - (void)testBranchEventRequestArchive { - (void)testBranchOpenRequestArchive { BranchOpenRequest *request = [[BranchOpenRequest alloc] initWithCallback:nil]; - request.urlString = @"https://branch.io"; + request.linkParams = [[BranchOpenRequestLinkParams alloc] init]; + request.linkParams.referringURL = @"https://branch.io"; // archive the event NSError *error = nil; @@ -88,14 +89,15 @@ - (void)testBranchOpenRequestArchive { XCTAssertTrue([object isKindOfClass:BranchOpenRequest.class]); BranchOpenRequest *unarchivedRequest = (BranchOpenRequest *)object; - XCTAssertTrue([request.urlString isEqualToString:unarchivedRequest.urlString]); + XCTAssertTrue([request.linkParams.referringURL isEqualToString:unarchivedRequest.linkParams.referringURL]); XCTAssertNil(unarchivedRequest.callback); XCTAssertTrue([@"open" isEqualToString:[unarchivedRequest getActionName]]); } - (void)testBranchInstallRequestArchive { BranchInstallRequest *request = [[BranchInstallRequest alloc] initWithCallback:nil]; - request.urlString = @"https://branch.io"; + request.linkParams = [[BranchOpenRequestLinkParams alloc] init]; + request.linkParams.referringURL = @"https://branch.io"; // archive the event NSError *error = nil; @@ -112,7 +114,7 @@ - (void)testBranchInstallRequestArchive { XCTAssertTrue([object isKindOfClass:BranchInstallRequest.class]); BranchInstallRequest *unarchivedRequest = (BranchInstallRequest *)object; - XCTAssertTrue([request.urlString isEqualToString:unarchivedRequest.urlString]); + XCTAssertTrue([request.linkParams.referringURL isEqualToString:unarchivedRequest.linkParams.referringURL]); XCTAssertNil(unarchivedRequest.callback); XCTAssertTrue([@"install" isEqualToString:[unarchivedRequest getActionName]]); } diff --git a/Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m b/Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m index 5269501b3..bf76adc51 100644 --- a/Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m +++ b/Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m @@ -243,13 +243,6 @@ - (void)testSetLastStrongMatchDate { XCTAssertEqualObjects(expectedDate, actualDate); } -- (void)testSetAppVersion { - NSString *expectedVersion = @"1.0.0"; - [self.prefHelper setAppVersion: expectedVersion]; - - NSString *actualVersion = [self.prefHelper appVersion]; - XCTAssertEqualObjects(expectedVersion, actualVersion); -} - (void)testSetLocalUrl { NSString *expectedLocalURL = @"https://local.example.com"; @@ -364,14 +357,8 @@ - (void)testClearTrackingInformation { [self.prefHelper clearTrackingInformation]; XCTAssertNil(self.prefHelper.sessionID); - XCTAssertNil(self.prefHelper.linkClickIdentifier); - XCTAssertNil(self.prefHelper.spotlightIdentifier); - XCTAssertNil(self.prefHelper.referringURL); - XCTAssertNil(self.prefHelper.universalLinkUrl); - XCTAssertNil(self.prefHelper.initialReferrer); XCTAssertNil(self.prefHelper.installParams); XCTAssertNil(self.prefHelper.sessionParams); - XCTAssertNil(self.prefHelper.externalIntentURI); XCTAssertNil(self.prefHelper.savedAnalyticsData); XCTAssertNil(self.prefHelper.previousAppBuildDate); XCTAssertEqual(self.prefHelper.requestMetadataDictionary.count, 0); diff --git a/Branch-TestBed/Branch-SDK-Tests/BNCRequestFactoryTests.m b/Branch-TestBed/Branch-SDK-Tests/BNCRequestFactoryTests.m index c28d3b6e0..f9796c165 100644 --- a/Branch-TestBed/Branch-SDK-Tests/BNCRequestFactoryTests.m +++ b/Branch-TestBed/Branch-SDK-Tests/BNCRequestFactoryTests.m @@ -29,7 +29,7 @@ - (void)tearDown { - (void)testInitWithBranchKeyNil { BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:nil UUID:_requestUUID TimeStamp:_requestCreationTimeStamp]; - NSDictionary *json = [factory dataForInstallWithURLString:@"https://branch.io"]; + NSDictionary *json = [factory dataForInstallWithLinkParams:nil]; XCTAssertNotNil(json); // key is omitted when nil @@ -40,7 +40,7 @@ - (void)testInitWithBranchKeyNil { - (void)testInitWithBranchKeyEmpty { BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp]; - NSDictionary *json = [factory dataForInstallWithURLString:@"https://branch.io"]; + NSDictionary *json = [factory dataForInstallWithLinkParams:nil]; XCTAssertNotNil(json); // empty string is allowed @@ -52,7 +52,7 @@ - (void)testInitWithBranchKeyEmpty { - (void)testInitWithBranchKey { BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp]; - NSDictionary *json = [factory dataForInstallWithURLString:@"https://branch.io"]; + NSDictionary *json = [factory dataForInstallWithLinkParams:nil]; XCTAssertNotNil(json); XCTAssertTrue([@"key_abcd" isEqualToString:[json objectForKey:@"branch_key"]]); @@ -62,7 +62,7 @@ - (void)testInitWithBranchKey { - (void)testDataForInstall { BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp]; - NSDictionary *json = [factory dataForInstallWithURLString:@"https://branch.io"]; + NSDictionary *json = [factory dataForInstallWithLinkParams:nil]; XCTAssertNotNil(json); XCTAssertTrue([@"key_abcd" isEqualToString:[json objectForKey:@"branch_key"]]); @@ -79,7 +79,7 @@ - (void)testDataForInstall { - (void)testDataForOpen { BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp]; - NSDictionary *json = [factory dataForOpenWithURLString:@"https://branch.io"]; + NSDictionary *json = [factory dataForInstallWithLinkParams:nil]; XCTAssertNotNil(json); XCTAssertTrue([@"key_abcd" isEqualToString:[json objectForKey:@"branch_key"]]); diff --git a/Sources/BranchSDK/BNCAppGroupsData.m b/Sources/BranchSDK/BNCAppGroupsData.m index cb4908f86..f36420342 100644 --- a/Sources/BranchSDK/BNCAppGroupsData.m +++ b/Sources/BranchSDK/BNCAppGroupsData.m @@ -64,7 +64,7 @@ - (NSDate *)getDateForKey:(NSString *)key { return nil; } -- (void)saveAppClipData { +- (void)saveAppClipData:(NSString *) referringURL { if ([BNCSystemObserver isAppClip]) { BNCApplication *application = [BNCApplication currentApplication]; @@ -79,7 +79,7 @@ - (void)saveAppClipData { BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance]; - NSString *url = preferenceHelper.referringURL; + NSString *url = referringURL; NSString *token = preferenceHelper.randomizedDeviceToken; NSString *bundleToken = preferenceHelper.randomizedBundleToken; diff --git a/Sources/BranchSDK/BNCPreferenceHelper.m b/Sources/BranchSDK/BNCPreferenceHelper.m index 1acf209a0..7087257fa 100644 --- a/Sources/BranchSDK/BNCPreferenceHelper.m +++ b/Sources/BranchSDK/BNCPreferenceHelper.m @@ -73,7 +73,6 @@ @interface BNCPreferenceHelper () { NSOperationQueue *_persistPrefsQueue; NSString *_lastSystemBuildVersion; NSString *_browserUserAgentString; - NSString *_referringURL; } @property (strong, nonatomic) NSMutableDictionary *persistenceDict; @@ -90,20 +89,15 @@ @implementation BNCPreferenceHelper // since we override both setter and getter, these properties do not auto synthesize @synthesize lastRunBranchKey = _lastRunBranchKey, - appVersion = _appVersion, randomizedDeviceToken = _randomizedDeviceToken, sessionID = _sessionID, - spotlightIdentifier = _spotlightIdentifier, randomizedBundleToken = _randomizedBundleToken, - linkClickIdentifier = _linkClickIdentifier, userUrl = _userUrl, userIdentity = _userIdentity, sessionParams = _sessionParams, installParams = _installParams, - universalLinkUrl = _universalLinkUrl, initialReferrer = _initialReferrer, localUrl = _localUrl, - externalIntentURI = _externalIntentURI, isDebug = _isDebug, retryCount = _retryCount, retryInterval = _retryInterval, @@ -220,20 +214,6 @@ - (void)setLastStrongMatchDate:(NSDate *)lastStrongMatchDate { } } -- (NSString *)appVersion { - if (!_appVersion) { - _appVersion = [self readStringFromDefaults:BRANCH_PREFS_KEY_APP_VERSION]; - } - return _appVersion; -} - -- (void)setAppVersion:(NSString *)appVersion { - if (![_appVersion isEqualToString:appVersion]) { - _appVersion = appVersion; - [self writeObjectToDefaults:BRANCH_PREFS_KEY_APP_VERSION value:appVersion]; - } -} - - (NSString *)randomizedDeviceToken { if (!_randomizedDeviceToken) { NSString *tmp = [self readStringFromDefaults:BRANCH_PREFS_KEY_RANDOMIZED_DEVICE_TOKEN]; @@ -308,62 +288,6 @@ - (void)setUserIdentity:(NSString *)userIdentity { [self writeObjectToDefaults:BRANCH_PREFS_KEY_IDENTITY value:userIdentity]; } -- (NSString *)linkClickIdentifier { - return [self readStringFromDefaults:BRANCH_PREFS_KEY_LINK_CLICK_IDENTIFIER]; -} - -- (void)setLinkClickIdentifier:(NSString *)linkClickIdentifier { - [self writeObjectToDefaults:BRANCH_PREFS_KEY_LINK_CLICK_IDENTIFIER value:linkClickIdentifier]; -} - -- (NSString *)spotlightIdentifier { - return [self readStringFromDefaults:BRANCH_PREFS_KEY_SPOTLIGHT_IDENTIFIER]; -} - -- (void)setSpotlightIdentifier:(NSString *)spotlightIdentifier { - [self writeObjectToDefaults:BRANCH_PREFS_KEY_SPOTLIGHT_IDENTIFIER value:spotlightIdentifier]; -} - -- (NSString *)externalIntentURI { - @synchronized(self) { - if (!_externalIntentURI) { - _externalIntentURI = [self readStringFromDefaults:BRANCH_REQUEST_KEY_EXTERNAL_INTENT_URI]; - } - return _externalIntentURI; - } -} - -- (void)setExternalIntentURI:(NSString *)externalIntentURI { - @synchronized(self) { - if (externalIntentURI == nil || ![_externalIntentURI isEqualToString:externalIntentURI]) { - _externalIntentURI = externalIntentURI; - [self writeObjectToDefaults:BRANCH_REQUEST_KEY_EXTERNAL_INTENT_URI value:externalIntentURI]; - } - } -} - -- (NSString*) referringURL { - @synchronized (self) { - if (!_referringURL) _referringURL = [self readStringFromDefaults:@"referringURL"]; - return _referringURL; - } -} - -- (void) setReferringURL:(NSString *)referringURL { - @synchronized (self) { - _referringURL = [referringURL copy]; - [self writeObjectToDefaults:@"referringURL" value:_referringURL]; - } -} - -- (NSString *)universalLinkUrl { - return [self readStringFromDefaults:BRANCH_PREFS_KEY_UNIVERSAL_LINK_URL]; -} - -- (void)setUniversalLinkUrl:(NSString *)universalLinkUrl { - [self writeObjectToDefaults:BRANCH_PREFS_KEY_UNIVERSAL_LINK_URL value:universalLinkUrl]; -} - - (NSString *)localUrl { return [self readStringFromDefaults:BRANCH_PREFS_KEY_LOCAL_URL]; } @@ -618,18 +542,6 @@ - (void) setSavedURLPatternListVersion:(NSInteger)URLPatternListVersion { } } -- (BOOL) dropURLOpen { - @synchronized(self) { - return [self readBoolFromDefaults:@"dropURLOpen"]; - } -} - -- (void) setDropURLOpen:(BOOL)value { - @synchronized(self) { - [self writeBoolToDefaults:@"dropURLOpen" value:value]; - } -} - - (BOOL) trackingDisabled { @synchronized(self) { NSNumber *b = (id) [self readObjectFromDefaults:@"trackingDisabled"]; @@ -849,14 +761,8 @@ - (void) clearTrackingInformation { self.randomizedBundleToken = nil; */ self.sessionID = nil; - self.linkClickIdentifier = nil; - self.spotlightIdentifier = nil; - self.referringURL = nil; - self.universalLinkUrl = nil; - self.initialReferrer = nil; self.installParams = nil; self.sessionParams = nil; - self.externalIntentURI = nil; self.savedAnalyticsData = nil; self.previousAppBuildDate = nil; self.requestMetadataDictionary = nil; diff --git a/Sources/BranchSDK/BNCRequestFactory.m b/Sources/BranchSDK/BNCRequestFactory.m index 660152714..7d62a4692 100644 --- a/Sources/BranchSDK/BNCRequestFactory.m +++ b/Sources/BranchSDK/BNCRequestFactory.m @@ -34,6 +34,8 @@ #import "BNCSKAdNetwork.h" #import "BNCReferringURLUtility.h" #import "BNCPasteboard.h" +#import "BranchOpenRequest.h" +#import "BranchInstallRequest.h" @interface BNCRequestFactory() @@ -80,7 +82,7 @@ - (BOOL)isTrackingDisabled { return Branch.trackingDisabled; } -- (NSDictionary *)dataForInstallWithURLString:(NSString *)urlString { +- (NSDictionary *)dataForInstallWithLinkParams:(BranchOpenRequestLinkParams *) linkParams { NSMutableDictionary *json = [NSMutableDictionary new]; // All requests @@ -99,18 +101,18 @@ - (NSDictionary *)dataForInstallWithURLString:(NSString *)urlString { // Install and Open [self addDeveloperUserIDToJSON:json]; [self addSystemObserverDataToJSON:json]; - [self addPreferenceHelperDataToJSON:json]; + [self addOpenRequestDataToJSON:json fromLinkParams:linkParams]; [self addPartnerParametersToJSON:json]; [self addAppleReceiptSourceToJSON:json]; [self addTimestampsToJSON:json]; // Check if the urlString is a valid URL to ensure it's a universal link, not the external intent uri - if (urlString) { - NSURL *url = [NSURL URLWithString:urlString]; + if (linkParams.referringURL && !linkParams.dropURLOpen) { + NSURL *url = [NSURL URLWithString:linkParams.referringURL]; if (url && ([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"])) { - [self safeSetValue:urlString forKey:BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL onDict:json]; + [self safeSetValue:linkParams.referringURL forKey:BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL onDict:json]; } else { - [self safeSetValue:urlString forKey:BRANCH_REQUEST_KEY_EXTERNAL_INTENT_URI onDict:json]; + [self safeSetValue:linkParams.referringURL forKey:BRANCH_REQUEST_KEY_EXTERNAL_INTENT_URI onDict:json]; } } @@ -132,7 +134,7 @@ - (NSDictionary *)dataForInstallWithURLString:(NSString *)urlString { return json; } -- (NSDictionary *)dataForOpenWithURLString:(NSString *)urlString { +- (NSDictionary *)dataForOpenWithLinkParams:(BranchOpenRequestLinkParams *) linkParams{ NSMutableDictionary *json = [NSMutableDictionary new]; // All requests @@ -154,19 +156,19 @@ - (NSDictionary *)dataForOpenWithURLString:(NSString *)urlString { // Install and Open [self addDeveloperUserIDToJSON:json]; [self addSystemObserverDataToJSON:json]; - [self addPreferenceHelperDataToJSON:json]; + [self addOpenRequestDataToJSON:json fromLinkParams:linkParams]; [self addPartnerParametersToJSON:json]; [self addAppleReceiptSourceToJSON:json]; [self addTimestampsToJSON:json]; // Check if the urlString is a valid URL to ensure it's a universal link, not the external intent uri - if (urlString) { - NSURL *url = [NSURL URLWithString:urlString]; + if (linkParams.referringURL && !linkParams.dropURLOpen) { + NSURL *url = [NSURL URLWithString:linkParams.referringURL]; if (url && ([url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"])) { - [self safeSetValue:urlString forKey:BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL onDict:json]; + [self safeSetValue:linkParams.referringURL forKey:BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL onDict:json]; } else { - [self safeSetValue:urlString forKey:BRANCH_REQUEST_KEY_EXTERNAL_INTENT_URI onDict:json]; + [self safeSetValue:linkParams.referringURL forKey:BRANCH_REQUEST_KEY_EXTERNAL_INTENT_URI onDict:json]; } } @@ -303,15 +305,13 @@ - (void)addShortURLTokensToJSON:(NSMutableDictionary *)json isSpotlightRequest:( json[BRANCH_REQUEST_KEY_SESSION_ID] = self.preferenceHelper.sessionID; } -- (void)addPreferenceHelperDataToJSON:(NSMutableDictionary *)json { +- (void)addOpenRequestDataToJSON:(NSMutableDictionary *)json fromLinkParams:(BranchOpenRequestLinkParams *) linkParams{ json[BRANCH_REQUEST_KEY_DEBUG] = @(self.preferenceHelper.isDebug); - - [self safeSetValue:self.preferenceHelper.linkClickIdentifier forKey:BRANCH_REQUEST_KEY_LINK_IDENTIFIER onDict:json]; - [self safeSetValue:self.preferenceHelper.spotlightIdentifier forKey:BRANCH_REQUEST_KEY_SPOTLIGHT_IDENTIFIER onDict:json]; [self safeSetValue:self.preferenceHelper.initialReferrer forKey:BRANCH_REQUEST_KEY_INITIAL_REFERRER onDict:json]; - // This was only on opens before, cause it can't exist on install. - [self safeSetValue:self.preferenceHelper.externalIntentURI forKey:BRANCH_REQUEST_KEY_EXTERNAL_INTENT_URI onDict:json]; + [self safeSetValue:linkParams.linkClickIdentifier forKey:BRANCH_REQUEST_KEY_LINK_IDENTIFIER onDict:json]; + [self safeSetValue:linkParams.spotlightIdentifier forKey:BRANCH_REQUEST_KEY_SPOTLIGHT_IDENTIFIER onDict:json]; + } - (void)addSystemObserverDataToJSON:(NSMutableDictionary *)json { diff --git a/Sources/BranchSDK/BNCServerRequestQueue.m b/Sources/BranchSDK/BNCServerRequestQueue.m index c518fccce..84f69f20e 100755 --- a/Sources/BranchSDK/BNCServerRequestQueue.m +++ b/Sources/BranchSDK/BNCServerRequestQueue.m @@ -138,7 +138,7 @@ - (BranchOpenRequest *)findExistingInstallOrOpen { // Install subclasses open, so only need to check open // Request should not be the one added from archived queue - if ([request isKindOfClass:[BranchOpenRequest class]] && !((BranchOpenRequest *)request).isFromArchivedQueue) { + if ([request isKindOfClass:[BranchOpenRequest class]]) { return (BranchOpenRequest *)request; } } diff --git a/Sources/BranchSDK/Branch.m b/Sources/BranchSDK/Branch.m index 6d552fec7..7fccba72e 100644 --- a/Sources/BranchSDK/Branch.m +++ b/Sources/BranchSDK/Branch.m @@ -556,7 +556,7 @@ + (void)setTrackingDisabled:(BOOL)disabled { // Set the flag: [BNCPreferenceHelper sharedInstance].trackingDisabled = NO; // Initialize a Branch session: - [Branch.getInstance initUserSessionAndCallCallback:NO sceneIdentifier:nil urlString:nil reset:NO]; + [Branch.getInstance initUserSessionAndCallCallback:NO sceneIdentifier:nil urlParams:nil reset:NO]; } } } @@ -605,7 +605,7 @@ - (void)setConsumerProtectionAttributionLevel:(BranchAttributionLevel)level { [BNCPreferenceHelper sharedInstance].trackingDisabled = NO; // Initialize a Branch session: - [[Branch getInstance] initUserSessionAndCallCallback:NO sceneIdentifier:nil urlString:nil reset:true]; + [[Branch getInstance] initUserSessionAndCallCallback:NO sceneIdentifier:nil urlParams:nil reset:true]; } } @@ -704,20 +704,18 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options self.shouldAutomaticallyDeepLink = automaticallyDisplayController; // Check for Branch link in a push payload - NSString *pushURL = nil; + BranchOpenRequestLinkParams *params = [[BranchOpenRequestLinkParams alloc] init]; #if !TARGET_OS_TV if ([options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) { id branchUrlFromPush = [options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey][BRANCH_PUSH_NOTIFICATION_PAYLOAD_KEY]; if ([branchUrlFromPush isKindOfClass:[NSString class]]) { - self.preferenceHelper.universalLinkUrl = branchUrlFromPush; - self.preferenceHelper.referringURL = branchUrlFromPush; - pushURL = (NSString *)branchUrlFromPush; + params.referringURL = (NSString *)branchUrlFromPush; } } #endif - if(pushURL || [[options objectForKey:@"BRANCH_DEFER_INIT_FOR_PLUGIN_RUNTIME_KEY"] isEqualToNumber:@1] || (![options.allKeys containsObject:UIApplicationLaunchOptionsURLKey] && ![options.allKeys containsObject:UIApplicationLaunchOptionsUserActivityDictionaryKey]) ) { - [self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:pushURL reset:NO]; + if(params.referringURL || [[options objectForKey:@"BRANCH_DEFER_INIT_FOR_PLUGIN_RUNTIME_KEY"] isEqualToNumber:@1] || (![options.allKeys containsObject:UIApplicationLaunchOptionsURLKey] && ![options.allKeys containsObject:UIApplicationLaunchOptionsUserActivityDictionaryKey]) ) { + [self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlParams:params reset:NO]; } } @@ -764,14 +762,12 @@ - (BOOL)handleDeepLink:(NSURL *)url sceneIdentifier:(NSString *)sceneIdentifier if (!pattern) { pattern = [self.userURLFilter patternMatchingURL:url]; } + if (pattern) { - self.preferenceHelper.dropURLOpen = YES; - - NSString *urlString = [url absoluteString]; - self.preferenceHelper.externalIntentURI = urlString; - self.preferenceHelper.referringURL = urlString; - - [self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlString:nil reset:YES]; + BranchOpenRequestLinkParams *params = [[BranchOpenRequestLinkParams alloc] init]; + params.dropURLOpen = YES; + params.referringURL = [url absoluteString];; + [self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlParams:params reset:YES]; return NO; } @@ -785,27 +781,23 @@ - (BOOL)handleDeepLink:(NSURL *)url sceneIdentifier:(NSString *)sceneIdentifier - (BOOL)handleSchemeDeepLink_private:(NSURL*)url sceneIdentifier:(NSString *)sceneIdentifier { BOOL handled = NO; - self.preferenceHelper.referringURL = nil; - [[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Set referringURL to %@", self.preferenceHelper.referringURL] error:nil]; + BranchOpenRequestLinkParams *openRequestParams = [[BranchOpenRequestLinkParams alloc] init]; if (url && ![url isEqual:[NSNull null]]) { NSString *urlScheme = [url scheme]; - // save the incoming url in the preferenceHelper in the externalIntentURI field if ([self.allowedSchemeList count]) { for (NSString *scheme in self.allowedSchemeList) { if (urlScheme && [scheme isEqualToString:urlScheme]) { - self.preferenceHelper.externalIntentURI = [url absoluteString]; - self.preferenceHelper.referringURL = [url absoluteString]; - [[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Allowed scheme list, set externalIntentURI and referringURL to %@", [url absoluteString]] error:nil]; + openRequestParams.referringURL = [url absoluteString]; + [[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Allowed scheme list, set referringURL to %@", [url absoluteString]] error:nil]; break; } } } else { - self.preferenceHelper.externalIntentURI = [url absoluteString]; - self.preferenceHelper.referringURL = [url absoluteString]; - [[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Set externalIntentURI and referringURL to %@", [url absoluteString]] error:nil]; + openRequestParams.referringURL = [url absoluteString]; + [[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Set referringURL to %@", [url absoluteString]] error:nil]; } NSString *query = [url fragment]; @@ -816,10 +808,10 @@ - (BOOL)handleSchemeDeepLink_private:(NSURL*)url sceneIdentifier:(NSString *)sce NSDictionary *params = [BNCEncodingUtils decodeQueryStringToDictionary:query]; if (params[@"link_click_id"]) { handled = YES; - self.preferenceHelper.linkClickIdentifier = params[@"link_click_id"]; + openRequestParams.linkClickIdentifier = params[@"link_click_id"]; } } - [self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlString:url.absoluteString reset:YES]; + [self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlParams:openRequestParams reset:YES]; return handled; } @@ -828,7 +820,7 @@ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { - return [self handleDeepLink:url sceneIdentifier:nil]; + return [self handleDeepLink:url sceneIdentifier:nil ]; } - (BOOL)sceneIdentifier:(NSString *)sceneIdentifier @@ -847,13 +839,14 @@ - (BOOL)application:(UIApplication *)application } - (BOOL)handleUniversalDeepLink_private:(NSString*)urlString sceneIdentifier:(NSString *)sceneIdentifier { + + BranchOpenRequestLinkParams *params = [[BranchOpenRequestLinkParams alloc] init]; if (urlString.length) { - self.preferenceHelper.universalLinkUrl = urlString; - self.preferenceHelper.referringURL = urlString; + params.referringURL = urlString; [[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"Set universalLinkUrl and referringURL to %@", urlString] error:nil]; } - [self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlString:urlString reset:YES]; + [self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlParams:params reset:YES]; return [Branch isBranchLink:urlString]; } @@ -873,7 +866,8 @@ - (BOOL)continueUserActivity:(NSUserActivity *)userActivity sceneIdentifier:(NSS } NSString *spotlightIdentifier = nil; - + BranchOpenRequestLinkParams *params = [[BranchOpenRequestLinkParams alloc] init]; + #if !TARGET_OS_TV // Check to see if a spotlight activity needs to be handled spotlightIdentifier = [self.contentDiscoveryManager spotlightIdentifierFromActivity:userActivity]; @@ -884,16 +878,16 @@ - (BOOL)continueUserActivity:(NSUserActivity *)userActivity sceneIdentifier:(NSS } else if (webURL != nil && [Branch isBranchLink:[webURL absoluteString]]) { return [self handleDeepLink:webURL sceneIdentifier:sceneIdentifier]; } else if (spotlightIdentifier) { - self.preferenceHelper.spotlightIdentifier = spotlightIdentifier; + params.spotlightIdentifier = spotlightIdentifier; } else { NSString *nonBranchSpotlightIdentifier = [self.contentDiscoveryManager standardSpotlightIdentifierFromActivity:userActivity]; if (nonBranchSpotlightIdentifier) { - self.preferenceHelper.spotlightIdentifier = nonBranchSpotlightIdentifier; + params.spotlightIdentifier = nonBranchSpotlightIdentifier; } } #endif - - [self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlString:userActivity.webpageURL.absoluteString reset:YES]; + params.referringURL = userActivity.webpageURL.absoluteString; + [self initUserSessionAndCallCallback:YES sceneIdentifier:sceneIdentifier urlParams:params reset:YES]; return spotlightIdentifier != nil; } @@ -1517,7 +1511,6 @@ + (Branch *)getInstanceInternal:(NSString *)key { // Note: Link Click Identifier is not cleared because of the potential for that to mess up a deep link if (preferenceHelper.lastRunBranchKey && ![key isEqualToString:preferenceHelper.lastRunBranchKey]) { [[BranchLogger shared] logWarning:@"The Branch Key has changed, clearing relevant items." error:nil]; - preferenceHelper.appVersion = nil; preferenceHelper.randomizedDeviceToken = nil; preferenceHelper.sessionID = nil; preferenceHelper.randomizedBundleToken = nil; @@ -1794,7 +1787,7 @@ - (void)applicationDidBecomeActive { if (!Branch.trackingDisabled && self.initializationStatus != BNCInitStatusInitialized && !installOrOpenInQueue) { [[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"applicationDidBecomeActive trackingDisabled %d initializationStatus %d installOrOpenInQueue %d", Branch.trackingDisabled, self.initializationStatus, installOrOpenInQueue] error:nil]; - [self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlString:nil reset:NO]; + [self initUserSessionAndCallCallback:YES sceneIdentifier:nil urlParams:nil reset:NO]; } }); } @@ -2021,25 +2014,28 @@ - (void)notifyNativeToInit { - (void)initSafetyCheck { if (self.initializationStatus == BNCInitStatusUninitialized) { [[BranchLogger shared] logDebug:@"Branch avoided an error by preemptively initializing." error:nil]; - [self initUserSessionAndCallCallback:NO sceneIdentifier:nil urlString:nil reset:NO]; + [self initUserSessionAndCallCallback:NO sceneIdentifier:nil urlParams:nil reset:NO]; } } -- (void)initUserSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier urlString:(NSString *)urlString reset:(BOOL)reset { +- (void)initUserSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier urlParams:(BranchOpenRequestLinkParams *)params reset:(BOOL)reset { @synchronized (self) { if (self.deferInitForPluginRuntime) { - if (urlString) { + if ( params.referringURL) { [[BranchLogger shared] logDebug:@"Branch init is deferred, caching link" error:nil]; - self.cachedURLString = urlString; + self.cachedURLString = params.referringURL; } else { [[BranchLogger shared] logDebug:@"Branch init is deferred, ignoring lifecycle call without a link" error:nil]; } return; } else { - if (!urlString && self.cachedURLString) { - urlString = self.cachedURLString; - [[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Using cached link: %@", urlString] error:nil]; + if (self.cachedURLString && !params.referringURL) { + if(!params){ + params = [[BranchOpenRequestLinkParams alloc] init]; + } + params.referringURL = self.cachedURLString; + [[BranchLogger shared] logDebug:[NSString stringWithFormat:@"Using cached link: %@", params.referringURL] error:nil]; } self.cachedURLString = nil; } @@ -2051,7 +2047,7 @@ - (void)initUserSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSStr // If the session is not yet initialized OR // If the session is already initialized or is initializing but we need to reset it. if ( reset || self.initializationStatus == BNCInitStatusUninitialized) { - [self initializeSessionAndCallCallback:callCallback sceneIdentifier:sceneIdentifier urlString:urlString]; + [self initializeSessionAndCallCallback:callCallback sceneIdentifier:sceneIdentifier urlParams:params]; } // If the session was initialized, but callCallback was specified, do so. else if (callCallback && self.initializationStatus == BNCInitStatusInitialized) { @@ -2072,10 +2068,10 @@ - (void)initUserSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSStr } // only called from initUserSessionAndCallCallback! -- (void)initializeSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier urlString:(NSString *)urlString { +- (void)initializeSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier urlParams:(BranchOpenRequestLinkParams *)params { - // BranchDelegate willStartSessionWithURL notification - NSURL *URL = (self.preferenceHelper.referringURL.length) ? [NSURL URLWithString:self.preferenceHelper.referringURL] : nil; + // BranchDelegate willStartSessionWithURL notification + NSURL *URL = (params.referringURL.length) ? [NSURL URLWithString:params.referringURL] : nil; if ([self.delegate respondsToSelector:@selector(branch:willStartSessionWithURL:)]) { [self.delegate branch:self willStartSessionWithURL:URL]; } @@ -2090,9 +2086,9 @@ - (void)initializeSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSS // callback on main, this is generally what the client expects and maintains our previous behavior dispatch_async(dispatch_get_main_queue(), ^ { if (error) { - [self handleInitFailure:error callCallback:callCallback sceneIdentifier:(NSString *)sceneIdentifier]; + [self handleInitFailure:error callCallback:callCallback sceneIdentifier:(NSString *)sceneIdentifier referringURL:params.referringURL]; } else { - [self handleInitSuccessAndCallCallback:callCallback sceneIdentifier:(NSString *)sceneIdentifier]; + [self handleInitSuccessAndCallCallback:callCallback sceneIdentifier:(NSString *)sceneIdentifier referringURL:params.referringURL]; } }); }; @@ -2110,26 +2106,26 @@ - (void)initializeSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSS req = [[BranchInstallRequest alloc] initWithCallback:initSessionCallback]; } req.callback = initSessionCallback; - req.urlString = urlString; + req.linkParams = params; [self.requestQueue insert:req at:0]; - NSString *message = [NSString stringWithFormat:@"Request %@ callback %@ link %@", req, req.callback, req.urlString]; + NSString *message = [NSString stringWithFormat:@"Request %@ callback %@ link params %@", req, req.callback, req.linkParams]; [[BranchLogger shared] logDebug:message error:nil]; } else { // new link arrival but an install or open is already on queue? need a new open for link resolution. - if (urlString) { + if (params.referringURL) { req = [[BranchOpenRequest alloc] initWithCallback:initSessionCallback]; req.callback = initSessionCallback; - req.urlString = urlString; + req.linkParams = params; // put it behind the one that's already on queue [self.requestQueue insert:req at:1]; [[BranchLogger shared] logDebug:@"Link resolution request" error:nil]; - NSString *message = [NSString stringWithFormat:@"Request %@ callback %@ link %@", req, req.callback, req.urlString]; + NSString *message = [NSString stringWithFormat:@"Request %@ callback %@ link params%@", req, req.callback, req.linkParams]; [[BranchLogger shared] logDebug:message error:nil]; } } @@ -2143,7 +2139,7 @@ - (void)initializeSessionAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSS } -- (void)handleInitSuccessAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier { +- (void)handleInitSuccessAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSString *)sceneIdentifier referringURL:(NSString *) referringURL{ self.initializationStatus = BNCInitStatusInitialized; [[BranchLogger shared] logVerbose:[NSString stringWithFormat:@"initializationStatus %ld", self.initializationStatus] error:nil]; @@ -2175,7 +2171,7 @@ - (void)handleInitSuccessAndCallCallback:(BOOL)callCallback sceneIdentifier:(NSS self.sceneSessionInitWithCallback(response, nil); } } - [self sendOpenNotificationWithLinkParameters:latestReferringParams error:nil]; + [self sendOpenNotificationWithLinkParameters:latestReferringParams referringURL:referringURL error:nil]; [self.urlFilter updatePatternListFromServerWithCompletion:nil]; @@ -2292,12 +2288,12 @@ - (void)automaticallyDeeplinkWithReferringParams:(NSDictionary *)latestReferring } } -- (void)sendOpenNotificationWithLinkParameters:(NSDictionary*)linkParameters +- (void)sendOpenNotificationWithLinkParameters:(NSDictionary*)linkParameters referringURL:(NSString *) referringURL error:(NSError*)error { - NSURL *originalURL = - (self.preferenceHelper.referringURL.length) - ? [NSURL URLWithString:self.preferenceHelper.referringURL] + NSURL *originalURL = + (referringURL.length) + ? [NSURL URLWithString:referringURL] : nil; BranchLinkProperties *linkProperties = nil; BranchUniversalObject *universalObject = nil; @@ -2333,8 +2329,6 @@ - (void)sendOpenNotificationWithLinkParameters:(NSDictionary*)linkParameters postNotificationName:BranchDidStartSessionNotification object:self userInfo:userInfo]; - - self.preferenceHelper.referringURL = nil; } - (void)removeViewControllerFromRootNavigationController:(UIViewController*)branchSharingController { @@ -2362,7 +2356,7 @@ - (void)presentSharingViewController:(UIViewController 0) { - referringURL = self.urlString; + if (self.linkParams.referringURL.length > 0) { + referringURL = self.linkParams.referringURL; } else { NSDictionary *sessionDataDict = [BNCEncodingUtils decodeJsonStringToDictionary:sessionData]; NSString *link = sessionDataDict[BRANCH_RESPONSE_KEY_BRANCH_REFERRING_LINK]; @@ -153,13 +153,6 @@ - (void)processResponse:(BNCServerResponse *)response error:(NSError *)error { } } - // Clear link identifiers so they don't get reused on the next open - preferenceHelper.linkClickIdentifier = nil; - preferenceHelper.spotlightIdentifier = nil; - preferenceHelper.universalLinkUrl = nil; - preferenceHelper.externalIntentURI = nil; - preferenceHelper.referringURL = referringURL; - preferenceHelper.dropURLOpen = NO; NSString *string = BNCStringFromWireFormat(data[BRANCH_RESPONSE_KEY_RANDOMIZED_BUNDLE_TOKEN]); if (!string) { @@ -174,7 +167,7 @@ - (void)processResponse:(BNCServerResponse *)response error:(NSError *)error { [BranchOpenRequest releaseOpenResponseLock]; if (self.isInstall) { - [[BNCAppGroupsData shared] saveAppClipData]; + [[BNCAppGroupsData shared] saveAppClipData: referringURL]; } #if !TARGET_OS_TV @@ -256,14 +249,15 @@ - (NSString *)getActionName { - (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; - if (!self) return self; - self.urlString = [decoder decodeObjectOfClass:NSString.class forKey:@"urlString"]; + if (self) { + self.linkParams = [decoder decodeObjectOfClass:BranchOpenRequestLinkParams.class forKey:@"urlString"]; + } return self; } - (void)encodeWithCoder:(NSCoder *)coder { [super encodeWithCoder:coder]; - [coder encodeObject:self.urlString forKey:@"urlString"]; + [coder encodeObject:self.linkParams forKey:@"urlString"]; } + (BOOL)supportsSecureCoding { @@ -318,3 +312,32 @@ + (void) releaseOpenResponseLock { } @end + +@implementation BranchOpenRequestLinkParams + +- (instancetype)init +{ + self = [super init]; + if (self) { + self.dropURLOpen = NO; + } + return self; +} + +- (instancetype)initWithCoder:(NSCoder *)decoder { + self = [super init]; + if (self) { + self.referringURL = [decoder decodeObjectOfClass:NSString.class forKey:@"referringURL"]; + } + return self; +} + +- (void)encodeWithCoder:(NSCoder *)coder { + [coder encodeObject:self.referringURL forKey:@"referringURL"]; +} + ++ (BOOL)supportsSecureCoding { + return YES; +} + +@end diff --git a/Sources/BranchSDK/Private/BNCAppGroupsData.h b/Sources/BranchSDK/Private/BNCAppGroupsData.h index 7626a2d46..5fcbc43ac 100644 --- a/Sources/BranchSDK/Private/BNCAppGroupsData.h +++ b/Sources/BranchSDK/Private/BNCAppGroupsData.h @@ -25,7 +25,7 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)shared; // saves app clip data when appropriate -- (void)saveAppClipData; +- (void)saveAppClipData:(NSString *) referringURL ; // loads app clip data - (BOOL)loadAppClipData; diff --git a/Sources/BranchSDK/Private/BNCRequestFactory.h b/Sources/BranchSDK/Private/BNCRequestFactory.h index 697154b63..187dcc818 100644 --- a/Sources/BranchSDK/Private/BNCRequestFactory.h +++ b/Sources/BranchSDK/Private/BNCRequestFactory.h @@ -7,6 +7,7 @@ // #import +#import "BranchOpenRequest.h" NS_ASSUME_NONNULL_BEGIN @@ -23,8 +24,8 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithBranchKey:(NSString *)key UUID:(NSString *)requestUUID TimeStamp:(NSNumber *)requestTimeStamp NS_DESIGNATED_INITIALIZER; - (instancetype)init NS_UNAVAILABLE; -- (NSDictionary *)dataForInstallWithURLString:(nullable NSString *)urlString; -- (NSDictionary *)dataForOpenWithURLString:(nullable NSString *)urlString; +- (NSDictionary *)dataForInstallWithLinkParams:(BranchOpenRequestLinkParams *) linkParams; +- (NSDictionary *)dataForOpenWithLinkParams:(BranchOpenRequestLinkParams *) linkParams; // Event data is passed in - (NSDictionary *)dataForEventWithEventDictionary:(NSMutableDictionary *)dictionary; diff --git a/Sources/BranchSDK/Private/BranchOpenRequest.h b/Sources/BranchSDK/Private/BranchOpenRequest.h index ec5717cd6..95022cb1c 100644 --- a/Sources/BranchSDK/Private/BranchOpenRequest.h +++ b/Sources/BranchSDK/Private/BranchOpenRequest.h @@ -9,12 +9,18 @@ #import "BNCServerRequest.h" #import "BNCCallbacks.h" +@interface BranchOpenRequestLinkParams : NSObject +@property (copy, nonatomic) NSString *linkClickIdentifier; +@property (copy, nonatomic) NSString *spotlightIdentifier; +@property (copy, nonatomic) NSString *referringURL; // URL that triggered this install or open event +@property (assign, nonatomic) BOOL dropURLOpen; +@end + @interface BranchOpenRequest : BNCServerRequest -// URL that triggered this install or open event -@property (nonatomic, copy, readwrite) NSString *urlString; -@property (assign, nonatomic) BOOL isFromArchivedQueue; + @property (nonatomic, copy) callbackWithStatus callback; +@property (nonatomic, strong, readwrite) BranchOpenRequestLinkParams *linkParams; + (void) waitForOpenResponseLock; + (void) releaseOpenResponseLock; diff --git a/Sources/BranchSDK/Public/BNCPreferenceHelper.h b/Sources/BranchSDK/Public/BNCPreferenceHelper.h index 464d5508a..0265670b8 100644 --- a/Sources/BranchSDK/Public/BNCPreferenceHelper.h +++ b/Sources/BranchSDK/Public/BNCPreferenceHelper.h @@ -23,16 +23,12 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void); @property (copy, nonatomic) NSString *lastRunBranchKey; @property (strong, nonatomic) NSDate *lastStrongMatchDate; -@property (copy, nonatomic) NSString *appVersion; @property (copy, nonatomic) NSString *randomizedDeviceToken; @property (copy, nonatomic) NSString *randomizedBundleToken; @property (copy, nonatomic) NSString *anonID; @property (copy, nonatomic) NSString *sessionID; -@property (copy, nonatomic) NSString *linkClickIdentifier; -@property (copy, nonatomic) NSString *spotlightIdentifier; -@property (copy, nonatomic) NSString *universalLinkUrl; @property (copy, nonatomic) NSString *initialReferrer; @property (copy, nonatomic) NSString *userUrl; @property (copy, nonatomic) NSString *localUrl; @@ -46,11 +42,9 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void); @property (assign, nonatomic) NSInteger retryCount; @property (assign, nonatomic) NSTimeInterval retryInterval; @property (assign, nonatomic) NSTimeInterval timeout; -@property (copy, nonatomic) NSString *externalIntentURI; @property (strong, nonatomic) NSMutableDictionary *savedAnalyticsData; @property (copy, nonatomic) NSString *lastSystemBuildVersion; @property (copy, nonatomic) NSString *browserUserAgentString; -@property (copy, nonatomic) NSString *referringURL; @property (assign, nonatomic) BOOL limitFacebookTracking; @property (strong, nonatomic) NSDate *previousAppBuildDate; @property (assign, nonatomic, readwrite) BOOL disableAdNetworkCallouts; @@ -58,7 +52,6 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void); @property (nonatomic, copy, readwrite) NSString *patternListURL; @property (strong, nonatomic) NSArray *savedURLPatternList; @property (assign, nonatomic) NSInteger savedURLPatternListVersion; -@property (assign, nonatomic) BOOL dropURLOpen; @property (assign, nonatomic) BOOL trackingDisabled;