You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.0 KiB

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
  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 "FIRAuthDefaultUIDelegate.h"
  19. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  20. #import <UIKit/UIKit.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 NS_DESIGNATED_INITIALIZER;
  30. @end
  31. @implementation FIRAuthDefaultUIDelegate {
  32. /** @var _viewController
  33. @brief The presenting view controller.
  34. */
  35. UIViewController *_viewController;
  36. }
  37. - (instancetype)initWithViewController:(nullable UIViewController *)viewController {
  38. self = [super init];
  39. if (self) {
  40. _viewController = viewController;
  41. }
  42. return self;
  43. }
  44. - (void)presentViewController:(UIViewController *)viewControllerToPresent
  45. animated:(BOOL)flag
  46. completion:(nullable void (^)(void))completion {
  47. [_viewController presentViewController:viewControllerToPresent
  48. animated:flag
  49. completion:completion];
  50. }
  51. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(nullable void (^)(void))completion {
  52. [_viewController dismissViewControllerAnimated:flag completion:completion];
  53. }
  54. + (id<FIRAuthUIDelegate>)defaultUIDelegate {
  55. // iOS App extensions should not call [UIApplication sharedApplication], even if UIApplication
  56. // responds to it.
  57. static Class applicationClass = nil;
  58. if (![GULAppEnvironmentUtil isAppExtension]) {
  59. Class cls = NSClassFromString(@"UIApplication");
  60. if (cls && [cls respondsToSelector:NSSelectorFromString(@"sharedApplication")]) {
  61. applicationClass = cls;
  62. }
  63. }
  64. UIViewController *topViewController;
  65. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  66. if (@available(iOS 13.0, tvOS 13.0, *)) {
  67. UIApplication *application = [applicationClass sharedApplication];
  68. NSSet<UIScene *> * connectedScenes = application.connectedScenes;
  69. for (UIScene *scene in connectedScenes) {
  70. if ([scene isKindOfClass:[UIWindowScene class]]) {
  71. UIWindowScene *windowScene = (UIWindowScene *)scene;
  72. for (UIWindow *window in windowScene.windows) {
  73. if (window.isKeyWindow) {
  74. topViewController = window.rootViewController;
  75. }
  76. }
  77. }
  78. }
  79. } else {
  80. UIApplication *application = [applicationClass sharedApplication];
  81. topViewController = application.keyWindow.rootViewController;
  82. }
  83. #else
  84. UIApplication *application = [applicationClass sharedApplication];
  85. topViewController = application.keyWindow.rootViewController;
  86. #endif
  87. while (true){
  88. if (topViewController.presentedViewController) {
  89. topViewController = topViewController.presentedViewController;
  90. } else if ([topViewController isKindOfClass:[UINavigationController class]]) {
  91. UINavigationController *nav = (UINavigationController *)topViewController;
  92. topViewController = nav.topViewController;
  93. } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
  94. UITabBarController *tab = (UITabBarController *)topViewController;
  95. topViewController = tab.selectedViewController;
  96. } else {
  97. break;
  98. }
  99. }
  100. return [[FIRAuthDefaultUIDelegate alloc] initWithViewController:topViewController];
  101. }
  102. @end
  103. NS_ASSUME_NONNULL_END
  104. #endif