@@ -496,46 +496,12 @@ - (nullable instancetype)initWithAPIKey:(NSString *)APIKey
496496 [GULSceneDelegateSwizzler proxyOriginalSceneDelegate ];
497497#endif // TARGET_OS_IOS
498498
499- #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
500- static Class applicationClass = nil ;
501- // iOS App extensions should not call [UIApplication sharedApplication], even if UIApplication
502- // responds to it.
503- if (![GULAppEnvironmentUtil isAppExtension ]) {
504- Class cls = NSClassFromString (@" UIApplication" );
505- if (cls && [cls respondsToSelector: @selector (sharedApplication )]) {
506- applicationClass = cls;
507- }
508- }
509- UIApplication *application = [applicationClass sharedApplication ];
510- if ([application respondsToSelector: @selector (isProtectedDataAvailable )]) {
511- if ([application isProtectedDataAvailable ]) {
512- [self protectedDataInitialization ];
513- } else {
514- // Add listener for UIApplicationProtectedDataDidBecomeAvailable.
515- self->_protectedDataDidBecomeAvailableObserver = [[NSNotificationCenter defaultCenter ]
516- addObserverForName: UIApplicationProtectedDataDidBecomeAvailable
517- object: nil
518- queue: nil
519- usingBlock: ^(NSNotification *notification) {
520- [self protectedDataInitialization ];
521- }];
522- }
523- } else {
524- [self protectedDataInitialization ];
525- }
526- #else
527499 [self protectedDataInitialization ];
528- #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
529500 }
530501 return self;
531502}
532503
533504- (void )protectedDataInitialization {
534- #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
535- [[NSNotificationCenter defaultCenter ] removeObserver: _protectedDataDidBecomeAvailableObserver
536- name: UIApplicationProtectedDataDidBecomeAvailable
537- object: nil ];
538- #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
539505 // Continue with the rest of initialization in the work thread.
540506 __weak FIRAuth *weakSelf = self;
541507 dispatch_async (FIRAuthGlobalWorkQueue (), ^{
@@ -564,17 +530,41 @@ - (void)protectedDataInitialization {
564530 [strongSelf updateCurrentUser: user byForce: NO savingToDisk: NO error: &error];
565531 self->_lastNotifiedUserToken = user.rawAccessToken ;
566532 } else {
533+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
534+ if (error.code == FIRAuthErrorCodeKeychainError) {
535+ // If there's a keychain error, assume it is due to the keychain being accessed
536+ // before the device is unlocked as a result of prewarming, and listen for the
537+ // UIApplicationProtectedDataDidBecomeAvailable notification.
538+ [strongSelf addProtectedDataDidBecomeAvailableObserver ];
539+ }
540+ #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
567541 FIRLogError (kFIRLoggerAuth , @" I-AUT000001" ,
568542 @" Error loading saved user when starting up: %@ " , error);
569543 }
570544 } else {
571545 [strongSelf internalUseUserAccessGroup: storedUserAccessGroup error: &error];
572546 if (error) {
547+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
548+ if (error.code == FIRAuthErrorCodeKeychainError) {
549+ // If there's a keychain error, assume it is due to the keychain being accessed
550+ // before the device is unlocked as a result of prewarming, and listen for the
551+ // UIApplicationProtectedDataDidBecomeAvailable notification.
552+ [strongSelf addProtectedDataDidBecomeAvailableObserver ];
553+ }
554+ #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
573555 FIRLogError (kFIRLoggerAuth , @" I-AUT000001" ,
574556 @" Error loading saved user when starting up: %@ " , error);
575557 }
576558 }
577559 } else {
560+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
561+ if (error.code == FIRAuthErrorCodeKeychainError) {
562+ // If there's a keychain error, assume it is due to the keychain being accessed
563+ // before the device is unlocked as a result of prewarming, and listen for the
564+ // UIApplicationProtectedDataDidBecomeAvailable notification.
565+ [strongSelf addProtectedDataDidBecomeAvailableObserver ];
566+ }
567+ #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
578568 FIRLogError (kFIRLoggerAuth , @" I-AUT000001" , @" Error loading saved user when starting up: %@ " ,
579569 error);
580570 }
@@ -613,6 +603,24 @@ - (void)protectedDataInitialization {
613603 });
614604}
615605
606+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
607+ - (void )addProtectedDataDidBecomeAvailableObserver {
608+ __weak FIRAuth *weakSelf = self;
609+ self->_protectedDataDidBecomeAvailableObserver = [[NSNotificationCenter defaultCenter ]
610+ addObserverForName: UIApplicationProtectedDataDidBecomeAvailable
611+ object: nil
612+ queue: nil
613+ usingBlock: ^(NSNotification *notification) {
614+ FIRAuth *strongSelf = weakSelf;
615+ [[NSNotificationCenter defaultCenter ]
616+ removeObserver: strongSelf->_protectedDataDidBecomeAvailableObserver
617+ name: UIApplicationProtectedDataDidBecomeAvailable
618+ object: nil ];
619+ [strongSelf protectedDataInitialization ];
620+ }];
621+ }
622+ #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
623+
616624- (void )dealloc {
617625 @synchronized (self) {
618626 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter ];
0 commit comments