|
34 | 34 | #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
|
35 | 35 | #import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleCallbackStatus.h"
|
36 | 36 | #import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleOnDisconnect.h"
|
37 |
| -#if !TARGET_OS_WATCH |
| 37 | +#if TARGET_OS_WATCH |
| 38 | +#import <WatchKit/WatchKit.h> |
| 39 | +#else |
38 | 40 | #import <SystemConfiguration/SystemConfiguration.h>
|
39 |
| -#endif // !TARGET_OS_WATCH |
| 41 | +#endif // TARGET_OS_WATCH |
40 | 42 | #import <dlfcn.h>
|
41 | 43 | #import <netinet/in.h>
|
42 | 44 |
|
@@ -166,6 +168,7 @@ - (id)initWithRepoInfo:(FRepoInfo *)repoInfo
|
166 | 168 | retryExponent:kPersistentConnReconnectMultiplier
|
167 | 169 | jitterFactor:0.7];
|
168 | 170 |
|
| 171 | + [self setupNotifications]; |
169 | 172 | // Make sure we don't actually connect until open is called
|
170 | 173 | [self interruptForReason:kFInterruptReasonWaitingForOpen];
|
171 | 174 | }
|
@@ -550,6 +553,77 @@ - (void)openNetworkConnectionWithContext:
|
550 | 553 | [self.realtime open];
|
551 | 554 | }
|
552 | 555 |
|
| 556 | +#if !TARGET_OS_WATCH |
| 557 | +static void reachabilityCallback(SCNetworkReachabilityRef ref, |
| 558 | + SCNetworkReachabilityFlags flags, void *info) { |
| 559 | + if (flags & kSCNetworkReachabilityFlagsReachable) { |
| 560 | + FFLog(@"I-RDB034014", |
| 561 | + @"Network became reachable. Trigger a connection attempt"); |
| 562 | + FPersistentConnection *self = (__bridge FPersistentConnection *)info; |
| 563 | + // Reset reconnect delay |
| 564 | + [self.retryHelper signalSuccess]; |
| 565 | + if (self->connectionState == ConnectionStateDisconnected) { |
| 566 | + [self tryScheduleReconnect]; |
| 567 | + } |
| 568 | + } else { |
| 569 | + FFLog(@"I-RDB034015", @"Network is not reachable"); |
| 570 | + } |
| 571 | +} |
| 572 | +#endif // !TARGET_OS_WATCH |
| 573 | + |
| 574 | +- (void)enteringForeground { |
| 575 | + dispatch_async(self.dispatchQueue, ^{ |
| 576 | + // Reset reconnect delay |
| 577 | + [self.retryHelper signalSuccess]; |
| 578 | + if (self->connectionState == ConnectionStateDisconnected) { |
| 579 | + [self tryScheduleReconnect]; |
| 580 | + } |
| 581 | + }); |
| 582 | +} |
| 583 | + |
| 584 | +- (void)setupNotifications { |
| 585 | +#if TARGET_OS_WATCH |
| 586 | + if (@available(watchOS 7.0, *)) { |
| 587 | + __weak FPersistentConnection *weakSelf = self; |
| 588 | + NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
| 589 | + [center addObserverForName:WKApplicationWillEnterForegroundNotification |
| 590 | + object:nil |
| 591 | + queue:nil |
| 592 | + usingBlock:^(NSNotification *_Nonnull note) { |
| 593 | + [weakSelf enteringForeground]; |
| 594 | + }]; |
| 595 | + } |
| 596 | +#else |
| 597 | + NSString *const *foregroundConstant = (NSString *const *)dlsym( |
| 598 | + RTLD_DEFAULT, "UIApplicationWillEnterForegroundNotification"); |
| 599 | + if (foregroundConstant) { |
| 600 | + [[NSNotificationCenter defaultCenter] |
| 601 | + addObserver:self |
| 602 | + selector:@selector(enteringForeground) |
| 603 | + name:*foregroundConstant |
| 604 | + object:nil]; |
| 605 | + } |
| 606 | + // An empty address is interpreted a generic internet access |
| 607 | + struct sockaddr_in zeroAddress; |
| 608 | + bzero(&zeroAddress, sizeof(zeroAddress)); |
| 609 | + zeroAddress.sin_len = sizeof(zeroAddress); |
| 610 | + zeroAddress.sin_family = AF_INET; |
| 611 | + reachability = SCNetworkReachabilityCreateWithAddress( |
| 612 | + kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress); |
| 613 | + SCNetworkReachabilityContext ctx = {0, (__bridge void *)(self), NULL, NULL, |
| 614 | + NULL}; |
| 615 | + if (SCNetworkReachabilitySetCallback(reachability, reachabilityCallback, |
| 616 | + &ctx)) { |
| 617 | + SCNetworkReachabilitySetDispatchQueue(reachability, self.dispatchQueue); |
| 618 | + } else { |
| 619 | + FFLog(@"I-RDB034016", |
| 620 | + @"Failed to set up network reachability monitoring"); |
| 621 | + CFRelease(reachability); |
| 622 | + reachability = NULL; |
| 623 | + } |
| 624 | +#endif // !TARGET_OS_WATCH |
| 625 | +} |
| 626 | + |
553 | 627 | - (void)sendAuthAndRestoreStateAfterComplete:(BOOL)restoreStateAfterComplete {
|
554 | 628 | NSAssert([self connected], @"Must be connected to send auth");
|
555 | 629 | NSAssert(self.authToken != nil,
|
|
0 commit comments