Skip to content

Commit 5c4d26e

Browse files
authored
Apple search ads weren't timing out on iPad during initialization. (#769)
Apple Search Ads weren't timing out on iPad which prevented Branch initialization. I added a 10 second timeout for search ads before initialization continues.
1 parent 6bbe4cb commit 5c4d26e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#import "NSString+Branch.h"
4040
#import "BNCSpotlightService.h"
4141
#import "../Fabric/FABKitProtocol.h" // Fabric
42+
#import <stdatomic.h>
4243

4344
NSString * const BRANCH_FEATURE_TAG_SHARE = @"share";
4445
NSString * const BRANCH_FEATURE_TAG_REFERRAL = @"referral";
@@ -76,6 +77,14 @@
7677
#define CSSearchableItemActivityIdentifier @"kCSSearchableItemActivityIdentifier"
7778
#endif
7879

80+
static inline dispatch_time_t BNCDispatchTimeFromSeconds(NSTimeInterval seconds) {
81+
return dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC);
82+
}
83+
84+
static inline void BNCAfterSecondsPerformBlock(NSTimeInterval seconds, dispatch_block_t block) {
85+
dispatch_after(BNCDispatchTimeFromSeconds(seconds), dispatch_get_main_queue(), block);
86+
}
87+
7988
#pragma mark - Load Categories
8089

8190
void ForceCategoriesToLoad(void);
@@ -881,8 +890,12 @@ - (BOOL)checkAppleSearchAdsAttribution {
881890
self.preferenceHelper.checkedAppleSearchAdAttribution = YES;
882891
self.asyncRequestCount++;
883892

893+
_Atomic __block BOOL hasBeenCalled = NO;
884894
void (^__nullable completionBlock)(NSDictionary *attrDetails, NSError *error) =
885895
^ void(NSDictionary *__nullable attrDetails, NSError *__nullable error) {
896+
BOOL localHasBeenCalled = atomic_exchange(&hasBeenCalled, YES);
897+
if (localHasBeenCalled) return;
898+
886899
self.asyncRequestCount--;
887900

888901
// If searchAdsDebugMode is on then force the result to a set value for testing:
@@ -929,6 +942,11 @@ - (BOOL)checkAppleSearchAdsAttribution {
929942
});
930943
};
931944

945+
// Set a expiration timer in case we don't get a call back (I'm looking at you, iPad):
946+
BNCAfterSecondsPerformBlock(10.0, ^ {
947+
completionBlock(nil, [NSError errorWithDomain:NSNetServicesErrorDomain code:NSNetServicesTimeoutError userInfo:nil]);
948+
});
949+
932950
((void (*)(id, SEL, void (^ __nullable)(NSDictionary *__nullable attrDetails, NSError * __nullable error)))
933951
[sharedClientInstance methodForSelector:requestAttribution])
934952
(sharedClientInstance, requestAttribution, completionBlock);

Branch-TestBed/Branch-SDK-Tests/BNCTestCase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import "Branch.h"
1212
#import "NSString+Branch.h"
1313

14-
static inline dispatch_time_t BNCDispatchTimeFromSeconds(NSTimeInterval seconds) {
14+
static inline dispatch_time_t BNCDispatchTimeFromSeconds(NSTimeInterval seconds) {
1515
return dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC);
1616
}
1717

0 commit comments

Comments
 (0)