Skip to content

Commit f60577f

Browse files
authored
watchOS support for RTDB (#7656)
* Initial attempt for watchOS Database support * watchOS Package manifest changes for SwiftPM testing. * Fix iOS compilation * Further iOS changes * Further watchOS changes * Reverted !WATCHOS && 0 change, fixed SharedTestUtils * Proper logging, using positive if statements * Properly handle lifecycle changes on watchOS sockets. Also only log errors if we haven't closed things intentionally. * Cleanup: - Removal from outdated sample - Fixed podspec - Fixed Swift Package * Actually restore SwiftPM XCScheme * Restore ExtensionDelegate.swift * Revert all changes to ExtensionDelegate.swift * Cleanup from code review. * Style * Added CHANGELOG entry. * Review comments. * Style.sh * Remove unnecessary wrappers for SwiftPM * Review feedback * Formatting
1 parent a3eb3b5 commit f60577f

File tree

15 files changed

+236
-80
lines changed

15 files changed

+236
-80
lines changed

FirebaseDatabase.podspec

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ Simplify your iOS development, grow your user base, and monetize more effectivel
1616
:tag => 'CocoaPods-' + s.version.to_s
1717
}
1818
s.social_media_url = 'https://twitter.com/Firebase'
19-
s.ios.deployment_target = '10.0'
20-
s.osx.deployment_target = '10.12'
21-
s.tvos.deployment_target = '10.0'
19+
20+
ios_deployment_target = '10.0'
21+
osx_deployment_target = '10.12'
22+
tvos_deployment_target = '10.0'
23+
watchos_deployment_target = '7.0'
24+
25+
s.ios.deployment_target = ios_deployment_target
26+
s.osx.deployment_target = osx_deployment_target
27+
s.tvos.deployment_target = tvos_deployment_target
28+
s.watchos.deployment_target = watchos_deployment_target
2229

2330
s.cocoapods_version = '>= 1.4.0'
2431
s.prefix_header_file = false
@@ -33,7 +40,10 @@ Simplify your iOS development, grow your user base, and monetize more effectivel
3340
]
3441
s.public_header_files = base_dir + 'Public/FirebaseDatabase/*.h'
3542
s.libraries = ['c++', 'icucore']
36-
s.frameworks = 'CFNetwork', 'Security', 'SystemConfiguration'
43+
s.ios.frameworks = 'CFNetwork', 'Security', 'SystemConfiguration'
44+
s.tvos.frameworks = 'CFNetwork', 'Security', 'SystemConfiguration'
45+
s.macos.frameworks = 'CFNetwork', 'Security', 'SystemConfiguration'
46+
s.watchos.frameworks = 'CFNetwork', 'Security', 'WatchKit'
3747
s.dependency 'leveldb-library', '~> 1.22'
3848
s.dependency 'FirebaseCore', '~> 7.0'
3949
s.pod_target_xcconfig = {
@@ -42,6 +52,7 @@ Simplify your iOS development, grow your user base, and monetize more effectivel
4252
}
4353

4454
s.test_spec 'unit' do |unit_tests|
55+
unit_tests.platforms = {:ios => ios_deployment_target, :osx => osx_deployment_target, :tvos => tvos_deployment_target}
4556
unit_tests.scheme = { :code_coverage => true }
4657
unit_tests.source_files = [
4758
'FirebaseDatabase/Tests/Unit/*.[mh]',
@@ -57,6 +68,7 @@ Simplify your iOS development, grow your user base, and monetize more effectivel
5768
end
5869

5970
s.test_spec 'integration' do |int_tests|
71+
int_tests.platforms = {:ios => ios_deployment_target, :osx => osx_deployment_target, :tvos => tvos_deployment_target}
6072
int_tests.scheme = { :code_coverage => true }
6173
int_tests.source_files = [
6274
'FirebaseDatabase/Tests/Integration/*.[mh]',

FirebaseDatabase/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Unreleased
2+
- [added] Added community support for watchOS. (#4556)
3+
14
# v7.7.0
25
- [fixed] Fix variable length array diagnostics warning (#7460).
36
# v7.5.1

FirebaseDatabase/Sources/Core/FPersistentConnection.m

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
#import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
3535
#import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleCallbackStatus.h"
3636
#import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleOnDisconnect.h"
37+
#if TARGET_OS_WATCH
38+
#import <WatchKit/WatchKit.h>
39+
#else
3740
#import <SystemConfiguration/SystemConfiguration.h>
41+
#endif // TARGET_OS_WATCH
3842
#import <dlfcn.h>
3943
#import <netinet/in.h>
4044

@@ -90,7 +94,9 @@ @interface FPersistentConnection () {
9094
NSTimeInterval reconnectDelay;
9195
NSTimeInterval lastConnectionAttemptTime;
9296
NSTimeInterval lastConnectionEstablishedTime;
97+
#if !TARGET_OS_WATCH
9398
SCNetworkReachabilityRef reachability;
99+
#endif // !TARGET_OS_WATCH
94100
}
95101

96102
- (int)getNextRequestNumber;
@@ -174,11 +180,13 @@ - (id)initWithRepoInfo:(FRepoInfo *)repoInfo
174180
}
175181

176182
- (void)dealloc {
183+
#if !TARGET_OS_WATCH
177184
if (reachability) {
178185
// Unschedule the notifications
179186
SCNetworkReachabilitySetDispatchQueue(reachability, NULL);
180187
CFRelease(reachability);
181188
}
189+
#endif // !TARGET_OS_WATCH
182190
}
183191

184192
#pragma mark -
@@ -537,6 +545,7 @@ - (void)openNetworkConnectionWithToken:(NSString *)token {
537545
[self.realtime open];
538546
}
539547

548+
#if !TARGET_OS_WATCH
540549
static void reachabilityCallback(SCNetworkReachabilityRef ref,
541550
SCNetworkReachabilityFlags flags, void *info) {
542551
if (flags & kSCNetworkReachabilityFlagsReachable) {
@@ -552,6 +561,7 @@ static void reachabilityCallback(SCNetworkReachabilityRef ref,
552561
FFLog(@"I-RDB034015", @"Network is not reachable");
553562
}
554563
}
564+
#endif // !TARGET_OS_WATCH
555565

556566
- (void)enteringForeground {
557567
dispatch_async(self.dispatchQueue, ^{
@@ -564,7 +574,18 @@ - (void)enteringForeground {
564574
}
565575

566576
- (void)setupNotifications {
567-
577+
#if TARGET_OS_WATCH
578+
if (@available(watchOS 7.0, *)) {
579+
__weak FPersistentConnection *weakSelf = self;
580+
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
581+
[center addObserverForName:WKApplicationWillEnterForegroundNotification
582+
object:nil
583+
queue:nil
584+
usingBlock:^(NSNotification *_Nonnull note) {
585+
[weakSelf enteringForeground];
586+
}];
587+
}
588+
#else
568589
NSString *const *foregroundConstant = (NSString *const *)dlsym(
569590
RTLD_DEFAULT, "UIApplicationWillEnterForegroundNotification");
570591
if (foregroundConstant) {
@@ -592,6 +613,7 @@ - (void)setupNotifications {
592613
CFRelease(reachability);
593614
reachability = NULL;
594615
}
616+
#endif // !TARGET_OS_WATCH
595617
}
596618

597619
- (void)sendAuthAndRestoreStateAfterComplete:(BOOL)restoreStateAfterComplete {
@@ -1234,6 +1256,10 @@ - (void)sendConnectStats {
12341256
if (self.config.persistenceEnabled) {
12351257
stats[@"persistence.osx.enabled"] = @1;
12361258
}
1259+
#elif TARGET_OS_WATCH
1260+
if (self.config.persistenceEnabled) {
1261+
stats[@"persistence.watchos.enabled"] = @1;
1262+
}
12371263
#endif
12381264
NSString *sdkVersion =
12391265
[[FIRDatabase sdkVersion] stringByReplacingOccurrencesOfString:@"."

FirebaseDatabase/Sources/Persistence/FLevelDBStorageEngine.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,16 @@ - (void)close {
267267
}
268268

269269
+ (NSString *)firebaseDir {
270-
#if TARGET_OS_IOS || TARGET_OS_TV
270+
#if TARGET_OS_IOS || TARGET_OS_WATCH
271271
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(
272272
NSDocumentDirectory, NSUserDomainMask, YES);
273273
NSString *documentsDir = [dirPaths objectAtIndex:0];
274274
return [documentsDir stringByAppendingPathComponent:@"firebase"];
275+
#elif TARGET_OS_TV
276+
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(
277+
NSCachesDirectory, NSUserDomainMask, YES);
278+
NSString *cachesDir = [dirPaths objectAtIndex:0];
279+
return [cachesDir stringByAppendingPathComponent:@"firebase"];
275280
#elif TARGET_OS_OSX
276281
return [NSHomeDirectory() stringByAppendingPathComponent:@".firebase"];
277282
#endif

FirebaseDatabase/Sources/Realtime/FWebSocketConnection.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
*/
1616

1717
#import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
18+
#if !TARGET_OS_WATCH
1819
#import "FirebaseDatabase/Sources/third_party/SocketRocket/FSRWebSocket.h"
20+
#endif // !TARGET_OS_WATCH
1921
#import <Foundation/Foundation.h>
2022

2123
@protocol FWebSocketDelegate;
2224

25+
#if !TARGET_OS_WATCH
2326
@interface FWebSocketConnection : NSObject <FSRWebSocketDelegate>
27+
#else
28+
@interface FWebSocketConnection : NSObject <NSURLSessionWebSocketDelegate>
29+
#endif // else !TARGET_OS_WATCH
2430

2531
@property(nonatomic, weak) id<FWebSocketDelegate> delegate;
2632

@@ -34,13 +40,19 @@
3440
- (void)start;
3541
- (void)send:(NSDictionary *)dictionary;
3642

43+
// Ignore FSRWebSocketDelegate calls on watchOS.
44+
#if !TARGET_OS_WATCH
3745
- (void)webSocket:(FSRWebSocket *)webSocket didReceiveMessage:(id)message;
38-
- (void)webSocketDidOpen:(FSRWebSocket *)webSocket;
46+
47+
// Exclude the `webSocket` argument since it isn't used in this codebase and it
48+
// allows for better code sharing with watchOS.
49+
- (void)webSocketDidOpen;
3950
- (void)webSocket:(FSRWebSocket *)webSocket didFailWithError:(NSError *)error;
4051
- (void)webSocket:(FSRWebSocket *)webSocket
4152
didCloseWithCode:(NSInteger)code
4253
reason:(NSString *)reason
4354
wasClean:(BOOL)wasClean;
55+
#endif // !TARGET_OS_WATCH
4456

4557
@end
4658

0 commit comments

Comments
 (0)