FIRAuthDefaultUIDelegate.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #import "FIRAuthDefaultUIDelegate.h"
  17. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  18. NS_ASSUME_NONNULL_BEGIN
  19. @interface FIRAuthDefaultUIDelegate ()
  20. /** @fn initWithViewController:
  21. @brief Initializes the instance with a view controller.
  22. @param viewController The view controller as the presenting view controller in @c
  23. FIRAuthUIDelegate.
  24. @return The initialized instance.
  25. */
  26. - (instancetype)initWithViewController:(nullable UIViewController *)viewController NS_DESIGNATED_INITIALIZER;
  27. @end
  28. @implementation FIRAuthDefaultUIDelegate {
  29. /** @var _viewController
  30. @brief The presenting view controller.
  31. */
  32. UIViewController *_viewController;
  33. }
  34. - (instancetype)initWithViewController:(nullable UIViewController *)viewController {
  35. self = [super init];
  36. if (self) {
  37. _viewController = viewController;
  38. }
  39. return self;
  40. }
  41. - (void)presentViewController:(UIViewController *)viewControllerToPresent
  42. animated:(BOOL)flag
  43. completion:(nullable void (^)(void))completion {
  44. [_viewController presentViewController:viewControllerToPresent
  45. animated:flag
  46. completion:completion];
  47. }
  48. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(nullable void (^)(void))completion {
  49. [_viewController dismissViewControllerAnimated:flag completion:completion];
  50. }
  51. + (id<FIRAuthUIDelegate>)defaultUIDelegate {
  52. // iOS App extensions should not call [UIApplication sharedApplication], even if UIApplication
  53. // responds to it.
  54. static Class applicationClass = nil;
  55. if (![GULAppEnvironmentUtil isAppExtension]) {
  56. Class cls = NSClassFromString(@"UIApplication");
  57. if (cls && [cls respondsToSelector:NSSelectorFromString(@"sharedApplication")]) {
  58. applicationClass = cls;
  59. }
  60. }
  61. UIApplication *application = [applicationClass sharedApplication];
  62. UIViewController *topViewController = application.keyWindow.rootViewController;
  63. while (true){
  64. if (topViewController.presentedViewController) {
  65. topViewController = topViewController.presentedViewController;
  66. } else if ([topViewController isKindOfClass:[UINavigationController class]]) {
  67. UINavigationController *nav = (UINavigationController *)topViewController;
  68. topViewController = nav.topViewController;
  69. } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
  70. UITabBarController *tab = (UITabBarController *)topViewController;
  71. topViewController = tab.selectedViewController;
  72. } else {
  73. break;
  74. }
  75. }
  76. return [[FIRAuthDefaultUIDelegate alloc] initWithViewController:topViewController];
  77. }
  78. @end
  79. NS_ASSUME_NONNULL_END