Skip to content

Commit 45f4b67

Browse files
committed
Adding watchOS support to Auth.
1 parent ca4a655 commit 45f4b67

11 files changed

+132
-63
lines changed

FirebaseAuth.podspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ supports email and password accounts, as well as several 3rd party authenticatio
2020
s.ios.deployment_target = '8.0'
2121
s.osx.deployment_target = '10.11'
2222
s.tvos.deployment_target = '10.0'
23+
s.watchos.deployment_target = '6.0'
2324

2425
s.cocoapods_version = '>= 1.4.0'
2526
s.static_framework = true
@@ -44,10 +45,12 @@ supports email and password accounts, as well as several 3rd party authenticatio
4445
s.dependency 'FirebaseAuthInterop', '~> 1.0'
4546
s.dependency 'FirebaseCore', '~> 6.6'
4647
s.dependency 'GoogleUtilities/AppDelegateSwizzler', '~> 6.5'
47-
s.dependency 'GoogleUtilities/Environment', '~> 6.5'
48+
s.dependency 'GoogleUtilities/Environment', '~> 6.6'
4849
s.dependency 'GTMSessionFetcher/Core', '~> 1.1'
4950

5051
s.test_spec 'unit' do |unit_tests|
52+
# Unit tests can't run on watchOS.
53+
unit_tests.platforms = {:ios => '8.0', :osx => '10.11', :tvos => '10.0'}
5154
unit_tests.source_files = 'FirebaseAuth/Tests/Unit/*.[mh]'
5255
unit_tests.osx.exclude_files = [
5356
'FirebaseAuth/Tests/Unit/FIRAuthAPNSTokenManagerTests.m',

FirebaseAuth/Sources/Auth/FIRAuth.m

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#import <GoogleUtilities/GULAppDelegateSwizzler.h>
3333
#import <GoogleUtilities/GULAppEnvironmentUtil.h>
3434
#import <GoogleUtilities/GULSceneDelegateSwizzler.h>
35+
#import <GoogleUtilities/GULSecureCoding.h>
3536

3637
#import "FirebaseAuth/Sources/Auth/FIRAuthDataResult_Internal.h"
3738
#import "FirebaseAuth/Sources/Auth/FIRAuthDispatcher.h"
@@ -2005,16 +2006,13 @@ - (BOOL)saveUser:(nullable FIRUser *)user error:(NSError *_Nullable *_Nullable)o
20052006
if (!user) {
20062007
success = [_keychainServices removeDataForKey:userKey error:outError];
20072008
} else {
2008-
// Encode the user object.
2009-
NSMutableData *archiveData = [NSMutableData data];
2010-
// iOS 12 deprecation
2011-
#pragma clang diagnostic push
2012-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
2013-
NSKeyedArchiver *archiver =
2014-
[[NSKeyedArchiver alloc] initForWritingWithMutableData:archiveData];
2015-
#pragma clang diagnostic pop
2016-
[archiver encodeObject:user forKey:userKey];
2017-
[archiver finishEncoding];
2009+
NSData *archiveData = [GULSecureCoding archivedDataWithObject:user
2010+
toKey:userKey
2011+
error:outError];
2012+
if (outError && *outError) {
2013+
// Error archiving the data.
2014+
return NO;
2015+
}
20182016

20192017
// Save the user object's encoded value.
20202018
success = [_keychainServices setData:archiveData forKey:userKey error:outError];
@@ -2058,13 +2056,15 @@ - (BOOL)getUser:(FIRUser *_Nullable *)outUser error:(NSError *_Nullable *_Nullab
20582056
*outUser = nil;
20592057
return YES;
20602058
}
2061-
// iOS 12 deprecation
2062-
#pragma clang diagnostic push
2063-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
2064-
NSKeyedUnarchiver *unarchiver =
2065-
[[NSKeyedUnarchiver alloc] initForReadingWithData:encodedUserData];
2066-
#pragma clang diagnostic pop
2067-
FIRUser *user = [unarchiver decodeObjectOfClass:[FIRUser class] forKey:userKey];
2059+
FIRUser *user = [GULSecureCoding unarchivedObjectOfClass:[FIRUser class]
2060+
fromData:encodedUserData
2061+
key:userKey
2062+
error:error];
2063+
if (error && *error) {
2064+
// An error occurred when unarchiving the user, operation failed.
2065+
return NO;
2066+
}
2067+
20682068
user.auth = self;
20692069
*outUser = user;
20702070

@@ -2230,13 +2230,10 @@ - (nullable FIRUser *)getStoredUserForAccessGroup:(NSString *_Nullable)accessGro
22302230
return nil;
22312231
}
22322232

2233-
// iOS 12 deprecation
2234-
#pragma clang diagnostic push
2235-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
2236-
NSKeyedUnarchiver *unarchiver =
2237-
[[NSKeyedUnarchiver alloc] initForReadingWithData:encodedUserData];
2238-
#pragma clang diagnostic pop
2239-
user = [unarchiver decodeObjectOfClass:[FIRUser class] forKey:userKey];
2233+
user = [GULSecureCoding unarchivedObjectOfClass:[FIRUser class]
2234+
fromData:encodedUserData
2235+
key:userKey
2236+
error:outError];
22402237
} else {
22412238
user = [self.storedUserManager getStoredUserForAccessGroup:self.userAccessGroup
22422239
projectIdentifier:self.app.options.APIKey

FirebaseAuth/Sources/Public/FIRAuth.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ NS_SWIFT_NAME(Auth)
456456

457457
- (void)signInWithEmail:(NSString *)email
458458
link:(NSString *)link
459-
completion:(nullable FIRAuthDataResultCallback)completion;
459+
completion:(nullable FIRAuthDataResultCallback)completion API_AVAILABLE(ios(8.0), macos(10.10));
460460

461461
/** @fn signInWithProvider:UIDelegate:completion:
462462
@brief Signs in using the provided auth provider instance.
@@ -736,7 +736,8 @@ NS_SWIFT_NAME(Auth)
736736
*/
737737
- (void)sendSignInLinkToEmail:(NSString *)email
738738
actionCodeSettings:(FIRActionCodeSettings *)actionCodeSettings
739-
completion:(nullable FIRSendSignInLinkToEmailCallback)completion;
739+
completion:(nullable FIRSendSignInLinkToEmailCallback)completion
740+
API_AVAILABLE(ios(8.0), macos(10.10));
740741

741742
/** @fn signOut:
742743
@brief Signs out the current user.

FirebaseAuth/Sources/Public/FIRGameCenterAuthProvider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ typedef void (^FIRGameCenterCredentialCallback)(FIRAuthCredential *_Nullable cre
4141
NS_SWIFT_NAME(GameCenterCredentialCallback);
4242

4343
/** @class FIRGameCenterAuthProvider
44-
@brief A concrete implementation of @c FIRAuthProvider for Game Center Sign In.
44+
@brief A concrete implementation of @c FIRAuthProvider for Game Center Sign In. Not available on
45+
watchOS.
4546
*/
47+
API_AVAILABLE(ios(8.0), macosx(10.11), tvos(10.0))
4648
NS_SWIFT_NAME(GameCenterAuthProvider)
4749
@interface FIRGameCenterAuthProvider : NSObject
4850

FirebaseAuth/Sources/SystemService/FIRAuthAPNSTokenManager.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
#if !TARGET_OS_OSX
1919

2020
#import <Foundation/Foundation.h>
21+
#if TARGET_OS_WATCH
22+
#import <WatchKit/WatchKit.h>
23+
#else
2124
#import <UIKit/UIKit.h>
25+
#endif // TARGET_OS_WATCH
2226

2327
@class FIRAuthAPNSToken;
2428

@@ -56,12 +60,21 @@ typedef void (^FIRAuthAPNSTokenCallback)(FIRAuthAPNSToken *_Nullable token,
5660
*/
5761
- (instancetype)init NS_UNAVAILABLE;
5862

59-
/** @fn initWithApplication:bundle
63+
#if TARGET_OS_WATCH
64+
/** @fn initWithApplication:
65+
@brief Initializes the instance.
66+
@param application The @c WKExtension to request the token from.
67+
@return The initialized instance.
68+
*/
69+
- (instancetype)initWithApplication:(WKExtension *)application NS_DESIGNATED_INITIALIZER;
70+
#else
71+
/** @fn initWithApplication:
6072
@brief Initializes the instance.
6173
@param application The @c UIApplication to request the token from.
6274
@return The initialized instance.
6375
*/
6476
- (instancetype)initWithApplication:(UIApplication *)application NS_DESIGNATED_INITIALIZER;
77+
#endif // TARGET_OS_WATCH
6578

6679
/** @fn getTokenWithCallback:
6780
@brief Attempts to get the APNs token.

FirebaseAuth/Sources/SystemService/FIRAuthAPNSTokenManager.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,29 @@
3939
static const NSTimeInterval kLegacyRegistrationTimeout = 30;
4040

4141
@implementation FIRAuthAPNSTokenManager {
42+
#if TARGET_OS_WATCH
43+
/** @var _application
44+
@brief The @c WKExtension to request the token from.
45+
*/
46+
WKExtension *_application;
47+
#else
4248
/** @var _application
4349
@brief The @c UIApplication to request the token from.
4450
*/
4551
UIApplication *_application;
52+
#endif // TARGET_OS_WATCH
4653

4754
/** @var _pendingCallbacks
4855
@brief The list of all pending callbacks for the APNs token.
4956
*/
5057
NSMutableArray<FIRAuthAPNSTokenCallback> *_pendingCallbacks;
5158
}
5259

60+
#if TARGET_OS_WATCH
61+
- (instancetype)initWithApplication:(WKExtension *)application {
62+
#else
5363
- (instancetype)initWithApplication:(UIApplication *)application {
64+
#endif // TARGET_OS_WATCH
5465
self = [super init];
5566
if (self) {
5667
_application = application;

FirebaseAuth/Sources/SystemService/FIRAuthAppCredentialManager.m

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredential.h"
2323
#import "FirebaseAuth/Sources/SystemService/FIRAuthAppCredentialManager.h"
2424

25+
#import <GoogleUtilities/GULSecureCoding.h>
26+
2527
NS_ASSUME_NONNULL_BEGIN
2628

2729
/** @var kKeychainDataKey
@@ -69,22 +71,24 @@ - (instancetype)initWithKeychain:(FIRAuthKeychainServices *)keychain {
6971
NSError *error;
7072
NSData *encodedData = [_keychainServices dataForKey:kKeychainDataKey error:&error];
7173
if (!error && encodedData) {
72-
// iOS 12 deprecation
73-
#pragma clang diagnostic push
74-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
75-
NSKeyedUnarchiver *unarchiver =
76-
[[NSKeyedUnarchiver alloc] initForReadingWithData:encodedData];
77-
#pragma clang diagnostic pop
78-
FIRAuthAppCredential *credential =
79-
[unarchiver decodeObjectOfClass:[FIRAuthAppCredential class] forKey:kFullCredentialKey];
80-
if ([credential isKindOfClass:[FIRAuthAppCredential class]]) {
74+
NSError *credentialError;
75+
FIRAuthAppCredential *credential = [GULSecureCoding unarchivedObjectOfClass:[FIRAuthAppCredential class]
76+
fromData:encodedData
77+
key:kFullCredentialKey
78+
error:&credentialError];
79+
80+
if ([credential isKindOfClass:[FIRAuthAppCredential class]] && !credentialError) {
8181
_credential = credential;
8282
}
83+
84+
NSError *receiptsError;
8385
NSSet<Class> *allowedClasses =
8486
[NSSet<Class> setWithObjects:[NSArray class], [NSString class], nil];
85-
NSArray<NSString *> *pendingReceipts = [unarchiver decodeObjectOfClasses:allowedClasses
86-
forKey:kPendingReceiptsKey];
87-
if ([pendingReceipts isKindOfClass:[NSArray class]]) {
87+
NSArray<NSString *> *pendingReceipts = [GULSecureCoding unarchivedObjectOfClasses:allowedClasses
88+
fromData:encodedData
89+
key:kPendingReceiptsKey
90+
error:&receiptsError];
91+
if ([pendingReceipts isKindOfClass:[NSArray class]] && !receiptsError) {
8892
_pendingReceipts = [pendingReceipts mutableCopy];
8993
}
9094
}

FirebaseAuth/Sources/SystemService/FIRAuthNotificationManager.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
#if !TARGET_OS_OSX
1919

2020
#import <Foundation/Foundation.h>
21+
#if TARGET_OS_WATCH
22+
#import <WatchKit/WatchKit.h>
23+
#else
2124
#import <UIKit/UIKit.h>
25+
#endif
2226

2327
@class FIRAuthAppCredentialManager;
2428

@@ -40,6 +44,17 @@ typedef void (^FIRAuthNotificationForwardingCallback)(BOOL isNotificationBeingFo
4044
*/
4145
@property(nonatomic, assign) NSTimeInterval timeout;
4246

47+
#if TARGET_OS_WATCH
48+
/** @fn initWithApplication:appCredentialManager:
49+
@brief Initializes the instance.
50+
@param application The extension.
51+
@param appCredentialManager The object to handle app credentials delivered via notification.
52+
@return The initialized instance.
53+
*/
54+
- (instancetype)initWithApplication:(WKExtension *)application
55+
appCredentialManager:(FIRAuthAppCredentialManager *)appCredentialManager
56+
NS_DESIGNATED_INITIALIZER;
57+
#else
4358
/** @fn initWithApplication:appCredentialManager:
4459
@brief Initializes the instance.
4560
@param application The application.
@@ -49,6 +64,7 @@ typedef void (^FIRAuthNotificationForwardingCallback)(BOOL isNotificationBeingFo
4964
- (instancetype)initWithApplication:(UIApplication *)application
5065
appCredentialManager:(FIRAuthAppCredentialManager *)appCredentialManager
5166
NS_DESIGNATED_INITIALIZER;
67+
#endif // TARGET_OS_WATCH
5268

5369
/** @fn init
5470
@brief please use initWithAppCredentialManager: instead.

FirebaseAuth/Sources/SystemService/FIRAuthNotificationManager.m

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@
5252
static const NSTimeInterval kProbingTimeout = 1;
5353

5454
@implementation FIRAuthNotificationManager {
55+
#if TARGET_OS_WATCH
56+
/** @var _application
57+
@brief The extension.
58+
*/
59+
WKExtension *_application;
60+
#else
5561
/** @var _application
5662
@brief The application.
5763
*/
5864
UIApplication *_application;
65+
#endif
5966

6067
/** @var _appCredentialManager
6168
@brief The object to handle app credentials delivered via notification.
@@ -78,7 +85,11 @@ @implementation FIRAuthNotificationManager {
7885
NSMutableArray<FIRAuthNotificationForwardingCallback> *_pendingCallbacks;
7986
}
8087

88+
#if TARGET_OS_WATCH
89+
- (instancetype)initWithApplication:(WKExtension *)application
90+
#else
8191
- (instancetype)initWithApplication:(UIApplication *)application
92+
#endif
8293
appCredentialManager:(FIRAuthAppCredentialManager *)appCredentialManager {
8394
self = [super init];
8495
if (self) {
@@ -107,23 +118,34 @@ - (void)checkNotificationForwardingWithCallback:(FIRAuthNotificationForwardingCa
107118
kNotificationProberKey : @"This fake notification should be forwarded to Firebase Auth."
108119
}
109120
};
121+
#if TARGET_OS_WATCH
122+
if ([self->_application.delegate respondsToSelector:@selector(didReceiveRemoteNotification:
123+
fetchCompletionHandler:)]) {
124+
[self->_application.delegate didReceiveRemoteNotification:proberNotification
125+
fetchCompletionHandler:^(WKBackgroundFetchResult result){
126+
}];
127+
}
128+
#else
110129
if ([self->_application.delegate
111130
respondsToSelector:@selector(application:
112131
didReceiveRemoteNotification:fetchCompletionHandler:)]) {
113132
[self->_application.delegate application:self->_application
114133
didReceiveRemoteNotification:proberNotification
115134
fetchCompletionHandler:^(UIBackgroundFetchResult result){
116135
}];
117-
#if !TARGET_OS_TV
118-
} else if ([self->_application.delegate
119-
respondsToSelector:@selector(application:didReceiveRemoteNotification:)]) {
136+
}
137+
#endif
138+
#if !TARGET_OS_TV && !TARGET_OS_WATCH
139+
// Broke `else if` style guidelines to make it more readable with the platform conditionals.
140+
else if ([self->_application.delegate respondsToSelector:@selector(application:
141+
didReceiveRemoteNotification:)]) {
120142
// iOS 10 deprecation
121143
#pragma clang diagnostic push
122144
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
123145
[self->_application.delegate application:self->_application
124146
didReceiveRemoteNotification:proberNotification];
125147
#pragma clang diagnostic pop
126-
#endif
148+
#endif // !TARGET_OS_TV && !TARGET_OS_WATCH
127149
} else {
128150
FIRLogWarning(kFIRLoggerAuth, @"I-AUT000015",
129151
@"The UIApplicationDelegate must handle remote notification for phone number "

FirebaseAuth/Sources/SystemService/FIRAuthStoredUserManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ NS_ASSUME_NONNULL_BEGIN
6666
we use API KEY.
6767
@param outError Return value for any error which occurs.
6868
*/
69-
- (FIRUser *)getStoredUserForAccessGroup:(NSString *)accessGroup
70-
projectIdentifier:(NSString *)projectIdentifier
71-
error:(NSError *_Nullable *_Nullable)outError;
69+
- (FIRUser *_Nullable)getStoredUserForAccessGroup:(NSString *)accessGroup
70+
projectIdentifier:(NSString *)projectIdentifier
71+
error:(NSError *_Nullable *_Nullable)outError;
7272

7373
/** @fn setStoredUser:forAccessGroup:projectID:error:
7474
@brief The setter of the user stored locally.

0 commit comments

Comments
 (0)