Skip to content

Commit f302649

Browse files
committed
Merge pull request #358 from BranchMetrics/check-fb-app-links-client-side
Check fb app links client side
2 parents 6b5e460 + e1e3207 commit f302649

File tree

8 files changed

+67
-11
lines changed

8 files changed

+67
-11
lines changed

Branch-SDK/Branch-SDK/BNCPreferenceHelper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
@property (assign, nonatomic) BOOL explicitlyRequestedReferrable;
3232
@property (assign, nonatomic) BOOL isReferrable;
3333
@property (assign, nonatomic) BOOL isDebug;
34-
@property (assign, nonatomic) BOOL isContinuingUserActivity;
34+
@property (assign, nonatomic) BOOL shouldWaitForInit;
3535
@property (assign, nonatomic) BOOL suppressWarningLogs;
36+
@property (assign, nonatomic) BOOL checkedFacebookAppLinks;
3637
@property (assign, nonatomic) NSInteger retryCount;
3738
@property (assign, nonatomic) NSTimeInterval retryInterval;
3839
@property (assign, nonatomic) NSTimeInterval timeout;

Branch-SDK/Branch-SDK/BNCPreferenceHelper.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
NSString * const BRANCH_PREFS_KEY_SESSION_ID = @"bnc_session_id";
2626
NSString * const BRANCH_PREFS_KEY_IDENTITY_ID = @"bnc_identity_id";
2727
NSString * const BRANCH_PREFS_KEY_IDENTITY = @"bnc_identity";
28+
NSString * const BRANCH_PREFS_KEY_CHECKED_FACEBOOK_APP_LINKS = @"bnc_checked_fb_app_links";
2829
NSString * const BRANCH_PREFS_KEY_LINK_CLICK_IDENTIFIER = @"bnc_link_click_identifier";
2930
NSString * const BRANCH_PREFS_KEY_SPOTLIGHT_IDENTIFIER = @"bnc_spotlight_identifier";
3031
NSString * const BRANCH_PREFS_KEY_UNIVERSAL_LINK_URL = @"bnc_universal_link_url";
@@ -73,12 +74,13 @@ @implementation BNCPreferenceHelper
7374
externalIntentURI = _externalIntentURI,
7475
isReferrable = _isReferrable,
7576
isDebug = _isDebug,
76-
isContinuingUserActivity = _isContinuingUserActivity,
77+
shouldWaitForInit = _shouldWaitForInit,
7778
suppressWarningLogs = _suppressWarningLogs,
7879
retryCount = _retryCount,
7980
retryInterval = _retryInterval,
8081
timeout = _timeout,
8182
lastStrongMatchDate = _lastStrongMatchDate,
83+
checkedFacebookAppLinks = _checkedFacebookAppLinks,
8284
requestMetadataDictionary = _requestMetadataDictionary;
8385

8486
+ (BNCPreferenceHelper *)preferenceHelper {
@@ -421,6 +423,16 @@ - (void)setUserUrl:(NSString *)userUrl {
421423
}
422424
}
423425

426+
- (BOOL)checkedFacebookAppLinks {
427+
_checkedFacebookAppLinks = [self readBoolFromDefaults:BRANCH_PREFS_KEY_CHECKED_FACEBOOK_APP_LINKS];
428+
return _checkedFacebookAppLinks;
429+
}
430+
431+
- (void)setCheckedFacebookAppLinks:(BOOL)checked {
432+
_checkedFacebookAppLinks = checked;
433+
[self writeBoolToDefaults:BRANCH_PREFS_KEY_CHECKED_FACEBOOK_APP_LINKS value:checked];
434+
}
435+
424436
- (BOOL)isReferrable {
425437
BOOL hasIdentity = self.identityID != nil;
426438

Branch-SDK/Branch-SDK/Branch.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,16 @@ typedef NS_ENUM(NSUInteger, BranchPromoCodeUsageType) {
382382
383383
@param debugParams dictionary of keystrings/valuestrings that will be added to response
384384
*/
385-
-(void) setDeepLinkDebugMode:(NSDictionary *)debugParams;
385+
-(void)setDeepLinkDebugMode:(NSDictionary *)debugParams;
386386

387387

388+
/**
389+
Register your Facebook SDK's FBSDKAppLinkUtility class to be used by Branch for deferred deep linking from their platform
390+
391+
@param FBSDKAppLinkUtility - call [FBSDKAppLinkUtility class] after importing #import <FBSDKCoreKit/FBSDKCoreKit.h>
392+
*/
393+
- (void)registerFacebookDeepLinkingClass:(id)FBSDKAppLinkUtility;
394+
388395
/**
389396
Specify the time to wait in seconds between retries in the case of a Branch server error
390397

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ @interface Branch() <BranchDeepLinkingControllerCompletionDelegate, FABKit>
8585
@property (assign, nonatomic) BOOL useCookieBasedMatching;
8686
@property (strong, nonatomic) NSDictionary *deepLinkDebugParams;
8787
@property (assign, nonatomic) BOOL accountForFacebookSDK;
88-
88+
@property (assign, nonatomic) id FBSDKAppLinkUtility;
8989

9090
@end
9191

@@ -340,16 +340,20 @@ - (void)initSessionWithLaunchOptions:(NSDictionary *)options isReferrable:(BOOL)
340340

341341
if ([BNCSystemObserver getOSVersion].integerValue >= 8) {
342342
if (![options objectForKey:UIApplicationLaunchOptionsURLKey] && ![options objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
343-
[self initUserSessionAndCallCallback:YES];
343+
// If Facebook SDK is present, call deferred app link check here
344+
if (![self checkFacebookAppLinks]) {
345+
[self initUserSessionAndCallCallback:YES];
346+
}
344347
}
345348
else if ([options objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey]) {
346-
self.preferenceHelper.isContinuingUserActivity = YES;
347349
if (self.accountForFacebookSDK) {
348350
id activity = [[options objectForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey] objectForKey:@"UIApplicationLaunchOptionsUserActivityKey"];
349351
if (activity && [activity isKindOfClass:[NSUserActivity class]]) {
350352
[self continueUserActivity:activity];
353+
return;
351354
}
352355
}
356+
self.preferenceHelper.shouldWaitForInit = YES;
353357
}
354358
}
355359
else if (![options objectForKey:UIApplicationLaunchOptionsURLKey]) {
@@ -367,7 +371,7 @@ - (void) setDeepLinkDebugMode:(NSDictionary *)debugParams {
367371

368372
- (BOOL)handleDeepLink:(NSURL *)url {
369373
BOOL handled = NO;
370-
if (url) {
374+
if (url && ![url isEqual:[NSNull null]]) {
371375
//always save the incoming url in the preferenceHelper in the externalIntentURI field
372376
self.preferenceHelper.externalIntentURI = [url absoluteString];
373377

@@ -393,7 +397,7 @@ - (BOOL)continueUserActivity:(NSUserActivity *)userActivity {
393397
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
394398
self.preferenceHelper.universalLinkUrl = [userActivity.webpageURL absoluteString];
395399
[self initUserSessionAndCallCallback:YES];
396-
self.preferenceHelper.isContinuingUserActivity = NO;
400+
self.preferenceHelper.shouldWaitForInit = NO;
397401

398402
id branchUniversalLinkDomains = [self.preferenceHelper getBranchUniversalLinkDomains];
399403
if ([branchUniversalLinkDomains isKindOfClass:[NSString class]] && [[userActivity.webpageURL absoluteString] containsString:branchUniversalLinkDomains]) {
@@ -430,7 +434,7 @@ - (BOOL)continueUserActivity:(NSUserActivity *)userActivity {
430434
}
431435
}
432436
[self initUserSessionAndCallCallback:YES];
433-
self.preferenceHelper.isContinuingUserActivity = NO;
437+
self.preferenceHelper.shouldWaitForInit = NO;
434438

435439
return spotlightIdentifier != nil;
436440
}
@@ -466,6 +470,33 @@ - (void)handlePushNotification:(NSDictionary *) userInfo {
466470
}
467471
}
468472

473+
# pragma mark - Facebook App Link check
474+
475+
- (void)registerFacebookDeepLinkingClass:(id)FBSDKAppLinkUtility {
476+
self.FBSDKAppLinkUtility = FBSDKAppLinkUtility;
477+
}
478+
479+
- (BOOL)checkFacebookAppLinks {
480+
if (self.FBSDKAppLinkUtility) {
481+
SEL fetchDeferredAppLink = NSSelectorFromString(@"fetchDeferredAppLink:");
482+
483+
if ([self.FBSDKAppLinkUtility methodForSelector:fetchDeferredAppLink]) {
484+
void (^__nullable completionBlock)(NSURL *appLink, NSError *error) = ^void(NSURL *__nullable appLink, NSError *__nullable error) {
485+
self.preferenceHelper.shouldWaitForInit = NO;
486+
[self handleDeepLink:appLink];
487+
};
488+
489+
self.preferenceHelper.checkedFacebookAppLinks = YES;
490+
self.preferenceHelper.shouldWaitForInit = YES;
491+
492+
((void (*)(id, SEL, void (^ __nullable)(NSURL *__nullable appLink, NSError * __nullable error)))[self.FBSDKAppLinkUtility methodForSelector:fetchDeferredAppLink])(self.FBSDKAppLinkUtility, fetchDeferredAppLink, completionBlock);
493+
494+
return YES;
495+
}
496+
}
497+
498+
return NO;
499+
}
469500

470501
#pragma mark - Deep Link Controller methods
471502

@@ -1204,7 +1235,7 @@ - (void)registerViewWithParams:(NSDictionary *)params andCallback:(callbackWithP
12041235
#pragma mark - Application State Change methods
12051236

12061237
- (void)applicationDidBecomeActive {
1207-
if (!self.isInitialized && !self.preferenceHelper.isContinuingUserActivity && ![self.requestQueue containsInstallOrOpen]) {
1238+
if (!self.isInitialized && !self.preferenceHelper.shouldWaitForInit && ![self.requestQueue containsInstallOrOpen]) {
12081239
[self initUserSessionAndCallCallback:YES];
12091240
}
12101241
}
@@ -1320,7 +1351,7 @@ - (void)processNextQueueItem {
13201351
#pragma mark - Session Initialization
13211352

13221353
- (void)initSessionIfNeededAndNotInProgress {
1323-
if (!self.isInitialized && !self.preferenceHelper.isContinuingUserActivity && ![self.requestQueue containsInstallOrOpen]) {
1354+
if (!self.isInitialized && !self.preferenceHelper.shouldWaitForInit && ![self.requestQueue containsInstallOrOpen]) {
13241355
[self initUserSessionAndCallCallback:NO];
13251356
}
13261357
}

Branch-SDK/Branch-SDK/BranchConstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern NSString * const BRANCH_REQUEST_KEY_OS;
5151
extern NSString * const BRANCH_REQUEST_KEY_OS_VERSION;
5252
extern NSString * const BRANCH_REQUEST_KEY_URI_SCHEME;
5353
extern NSString * const BRANCH_REQUEST_KEY_UPDATE;
54+
extern NSString * const BRANCH_REQUEST_KEY_CHECKED_FACEBOOK_APPLINKS;
5455
extern NSString * const BRANCH_REQUEST_KEY_LINK_IDENTIFIER;
5556
extern NSString * const BRANCH_REQUEST_KEY_SPOTLIGHT_IDENTIFIER;
5657
extern NSString * const BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL;

Branch-SDK/Branch-SDK/BranchConstants.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
NSString * const BRANCH_REQUEST_KEY_URI_SCHEME = @"uri_scheme";
5353
NSString * const BRANCH_REQUEST_KEY_UPDATE = @"update";
5454
NSString * const BRANCH_REQUEST_KEY_LINK_IDENTIFIER = @"link_identifier";
55+
NSString * const BRANCH_REQUEST_KEY_CHECKED_FACEBOOK_APPLINKS = @"facebook_app_link_checked";
5556
NSString * const BRANCH_REQUEST_KEY_SPOTLIGHT_IDENTIFIER = @"spotlight_identifier";
5657
NSString * const BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL = @"universal_link_url";
5758
NSString * const BRANCH_REQUEST_KEY_BRAND = @"brand";

Branch-SDK/Branch-SDK/Requests/BranchInstallRequest.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ - (void)makeRequest:(BNCServerInterface *)serverInterface key:(NSString *)key ca
2828
[self safeSetValue:[BNCSystemObserver getAppVersion] forKey:BRANCH_REQUEST_KEY_APP_VERSION onDict:params];
2929
[self safeSetValue:[BNCSystemObserver getDefaultUriScheme] forKey:BRANCH_REQUEST_KEY_URI_SCHEME onDict:params];
3030
[self safeSetValue:[BNCSystemObserver getUpdateState] forKey:BRANCH_REQUEST_KEY_UPDATE onDict:params];
31+
[self safeSetValue:[NSNumber numberWithBool:preferenceHelper.checkedFacebookAppLinks] forKey:BRANCH_REQUEST_KEY_CHECKED_FACEBOOK_APPLINKS onDict:params];
3132
[self safeSetValue:preferenceHelper.linkClickIdentifier forKey:BRANCH_REQUEST_KEY_LINK_IDENTIFIER onDict:params];
3233
[self safeSetValue:preferenceHelper.spotlightIdentifier forKey:BRANCH_REQUEST_KEY_SPOTLIGHT_IDENTIFIER onDict:params];
3334
[self safeSetValue:preferenceHelper.universalLinkUrl forKey:BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL onDict:params];

Branch-SDK/Branch-SDK/Requests/BranchOpenRequest.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ - (void)makeRequest:(BNCServerInterface *)serverInterface key:(NSString *)key ca
5151
[self safeSetValue:[BNCSystemObserver getAppVersion] forKey:BRANCH_REQUEST_KEY_APP_VERSION onDict:params];
5252
[self safeSetValue:[BNCSystemObserver getDefaultUriScheme] forKey:BRANCH_REQUEST_KEY_URI_SCHEME onDict:params];
5353
[self safeSetValue:[BNCSystemObserver getUpdateState] forKey:BRANCH_REQUEST_KEY_UPDATE onDict:params];
54+
[self safeSetValue:[NSNumber numberWithBool:preferenceHelper.checkedFacebookAppLinks] forKey:BRANCH_REQUEST_KEY_CHECKED_FACEBOOK_APPLINKS onDict:params];
5455
[self safeSetValue:preferenceHelper.linkClickIdentifier forKey:BRANCH_REQUEST_KEY_LINK_IDENTIFIER onDict:params];
5556
[self safeSetValue:preferenceHelper.spotlightIdentifier forKey:BRANCH_REQUEST_KEY_SPOTLIGHT_IDENTIFIER onDict:params];
5657
[self safeSetValue:preferenceHelper.universalLinkUrl forKey:BRANCH_REQUEST_KEY_UNIVERSAL_LINK_URL onDict:params];
@@ -108,6 +109,7 @@ - (void)processResponse:(BNCServerResponse *)response error:(NSError *)error {
108109
}
109110

110111
// Clear link identifiers so they don't get reused on the next open
112+
preferenceHelper.checkedFacebookAppLinks = NO;
111113
preferenceHelper.linkClickIdentifier = nil;
112114
preferenceHelper.spotlightIdentifier = nil;
113115
preferenceHelper.universalLinkUrl = nil;

0 commit comments

Comments
 (0)