Skip to content

Commit a844f98

Browse files
committed
SDK-777 add connectivity check to BranchEvent with callback
1 parent 44361df commit a844f98

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

Branch-SDK/Branch-SDK/BNCDeviceInfo.m

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929

3030
@interface BNCDeviceInfo()
3131

32-
@property (nonatomic, strong, readwrite) BNCReachability *reachability;
33-
3432
@property (nonatomic, copy, readwrite) NSString *randomId;
3533

3634
@end
@@ -66,9 +64,7 @@ - (void)loadDeviceInfo {
6664
BNCLocale *locale = [BNCLocale new];
6765
BNCTelephony *telephony = [BNCTelephony new];
6866
BNCDeviceSystem *deviceSystem = [BNCDeviceSystem new];
69-
70-
self.reachability = [BNCReachability new];
71-
67+
7268
// The random id is regenerated per app launch. This maintains existing behavior.
7369
self.randomId = [[NSUUID UUID] UUIDString];
7470
self.vendorId = [BNCSystemObserver getVendorId];
@@ -114,7 +110,7 @@ - (NSString *)localIPAddress {
114110
}
115111

116112
- (NSString *)connectionType {
117-
return [self.reachability reachabilityStatus];
113+
return [[BNCReachability shared] reachabilityStatus];
118114
}
119115

120116
- (NSString *)userAgentString {
@@ -160,7 +156,7 @@ - (NSDictionary *)v2dictionary {
160156
[dictionary bnc_safeSetObject:self.vendorId forKey:@"idfv"];
161157
[dictionary bnc_safeSetObject:self.advertiserId forKey:@"idfa"];
162158
}
163-
[dictionary bnc_safeSetObject:[BNCNetworkInterface localIPAddress] forKey:@"local_ip"];
159+
[dictionary bnc_safeSetObject:[self localIPAddress] forKey:@"local_ip"];
164160

165161
if (!self.isAdTrackingEnabled) {
166162
dictionary[@"limit_ad_tracking"] = @(YES);
@@ -184,7 +180,7 @@ - (NSDictionary *)v2dictionary {
184180
[dictionary bnc_safeSetObject:self.language forKey:@"language"];
185181
[dictionary bnc_safeSetObject:self.carrierName forKey:@"device_carrier"];
186182
[dictionary bnc_safeSetObject:[self connectionType] forKey:@"connection_type"];
187-
[dictionary bnc_safeSetObject:[BNCUserAgentCollector instance].userAgent forKey:@"user_agent"];
183+
[dictionary bnc_safeSetObject:[self userAgentString] forKey:@"user_agent"];
188184

189185
[dictionary bnc_safeSetObject:[BNCPreferenceHelper preferenceHelper].userIdentity forKey:@"developer_identity"];
190186
[dictionary bnc_safeSetObject:[BNCPreferenceHelper preferenceHelper].deviceFingerprintID forKey:@"device_fingerprint_id"];

Branch-SDK/Branch-SDK/BNCReachability.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ NS_ASSUME_NONNULL_BEGIN
1313
// Handles network connectivity and type information
1414
@interface BNCReachability : NSObject
1515

16+
+ (BNCReachability *)shared;
17+
1618
- (nullable NSString *)reachabilityStatus;
1719

1820
@end

Branch-SDK/Branch-SDK/BNCReachability.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ @interface BNCReachability()
2727
*/
2828
@implementation BNCReachability
2929

30+
+ (BNCReachability *)shared {
31+
static BNCReachability *reachability;
32+
static dispatch_once_t onceToken;
33+
dispatch_once(&onceToken, ^{
34+
reachability = [BNCReachability new];
35+
});
36+
return reachability;
37+
}
38+
3039
- (instancetype)init {
3140
self = [super init];
3241
if (self) {

Branch-SDK/Branch-SDK/BranchEvent.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "BranchEvent.h"
1010
#import "BNCLog.h"
1111
#import "BNCCallbackMap.h"
12+
#import "BNCReachability.h"
1213

1314
#pragma mark BranchStandardEvents
1415

@@ -224,17 +225,22 @@ - (NSDictionary*) dictionary {
224225
- (void)logEventWithCompletion:(void (^_Nullable)(NSString *statusMessage))completion {
225226
if (![_eventName isKindOfClass:[NSString class]] || _eventName.length == 0) {
226227
BNCLogError(@"Invalid event type '%@' or empty string.", NSStringFromClass(_eventName.class));
227-
228228
if (completion) {
229229
completion(@"Error: Invalid event type");
230230
}
231-
231+
return;
232+
}
233+
234+
// logEvent requests without a completion are automatically retried later
235+
if (completion != nil && [[BNCReachability shared] reachabilityStatus] == nil) {
236+
if (completion) {
237+
completion(@"Error: No connectivity");
238+
}
232239
return;
233240
}
234241

235242
NSDictionary *eventDictionary = [self buildEventDictionary];
236243
BranchEventRequest *request = [self buildRequestWithEventDictionary:eventDictionary];
237-
238244
[[BNCCallbackMap shared] storeRequest:request withCompletion:completion];
239245

240246
[[Branch getInstance] sendServerRequest:request];

0 commit comments

Comments
 (0)