FIRAuthDefaultUIDelegate.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <TargetConditionals.h>
  17. #if !TARGET_OS_OSX
  18. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  19. #import <UIKit/UIKit.h>
  20. #import "FirebaseAuth/Sources/Utilities/FIRAuthDefaultUIDelegate.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface FIRAuthDefaultUIDelegate ()
  23. /** @fn initWithViewController:
  24. @brief Initializes the instance with a view controller.
  25. @param viewController The view controller as the presenting view controller in @c
  26. FIRAuthUIDelegate.
  27. @return The initialized instance.
  28. */
  29. - (instancetype)initWithViewController:(nullable UIViewController *)viewController
  30. NS_DESIGNATED_INITIALIZER;
  31. @end
  32. @implementation FIRAuthDefaultUIDelegate {
  33. /** @var _viewController
  34. @brief The presenting view controller.
  35. */
  36. UIViewController *_viewController;
  37. }
  38. - (instancetype)initWithViewController:(nullable UIViewController *)viewController {
  39. self = [super init];
  40. if (self) {
  41. _viewController = viewController;
  42. }
  43. return self;
  44. }
  45. - (void)presentViewController:(UIViewController *)viewControllerToPresent
  46. animated:(BOOL)flag
  47. completion:(nullable void (^)(void))completion {
  48. [_viewController presentViewController:viewControllerToPresent
  49. animated:flag
  50. completion:completion];
  51. }
  52. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(nullable void (^)(void))completion {
  53. [_viewController dismissViewControllerAnimated:flag completion:completion];
  54. }
  55. + (id<FIRAuthUIDelegate>)defaultUIDelegate {
  56. // iOS App extensions should not call [UIApplication sharedApplication], even if UIApplication
  57. // responds to it.
  58. static Class applicationClass = nil;
  59. if (![GULAppEnvironmentUtil isAppExtension]) {
  60. Class cls = NSClassFromString(@"UIApplication");
  61. if (cls && [cls respondsToSelector:NSSelectorFromString(@"sharedApplication")]) {
  62. applicationClass = cls;
  63. }
  64. }
  65. UIViewController *topViewController;
  66. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  67. if (@available(iOS 13.0, tvOS 13.0, *)) {
  68. UIApplication *application = [applicationClass sharedApplication];
  69. NSSet<UIScene *> *connectedScenes = application.connectedScenes;
  70. for (UIScene *scene in connectedScenes) {
  71. if ([scene isKindOfClass:[UIWindowScene class]]) {
  72. UIWindowScene *windowScene = (UIWindowScene *)scene;
  73. for (UIWindow *window in windowScene.windows) {
  74. if (window.isKeyWindow) {
  75. topViewController = window.rootViewController;
  76. }
  77. }
  78. }
  79. }
  80. } else {
  81. UIApplication *application = [applicationClass sharedApplication];
  82. // iOS 13 deprecation
  83. #pragma clang diagnostic push
  84. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  85. topViewController = application.keyWindow.rootViewController;
  86. #pragma clang diagnostic pop
  87. }
  88. #else
  89. UIApplication *application = [applicationClass sharedApplication];
  90. topViewController = application.keyWindow.rootViewController;
  91. #endif
  92. while (true) {
  93. if (topViewController.presentedViewController) {
  94. topViewController = topViewController.presentedViewController;
  95. } else if ([topViewController isKindOfClass:[UINavigationController class]]) {
  96. UINavigationController *nav = (UINavigationController *)topViewController;
  97. topViewController = nav.topViewController;
  98. } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
  99. UITabBarController *tab = (UITabBarController *)topViewController;
  100. topViewController = tab.selectedViewController;
  101. } else {
  102. break;
  103. }
  104. }
  105. return [[FIRAuthDefaultUIDelegate alloc] initWithViewController:topViewController];
  106. }
  107. @end
  108. NS_ASSUME_NONNULL_END
  109. #endif