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.

81 lines
2.7 KiB

6 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. #import "FIRAuthDefaultUIDelegate.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. @interface FIRAuthDefaultUIDelegate ()
  19. /** @fn initWithViewController:
  20. @brief Initializes the instance with a view controller.
  21. @param viewController The view controller as the presenting view controller in @c
  22. FIRAuthUIDelegate.
  23. @return The initialized instance.
  24. */
  25. - (instancetype)initWithViewController:(UIViewController *)viewController NS_DESIGNATED_INITIALIZER;
  26. @end
  27. @implementation FIRAuthDefaultUIDelegate {
  28. /** @var _viewController
  29. @brief The presenting view controller.
  30. */
  31. UIViewController *_viewController;
  32. }
  33. - (instancetype)initWithViewController:(UIViewController *)viewController {
  34. self = [super init];
  35. if (self) {
  36. _viewController = viewController;
  37. }
  38. return self;
  39. }
  40. - (void)presentViewController:(UIViewController *)viewControllerToPresent
  41. animated:(BOOL)flag
  42. completion:(nullable void (^)(void))completion {
  43. [_viewController presentViewController:viewControllerToPresent
  44. animated:flag
  45. completion:completion];
  46. }
  47. - (void)dismissViewControllerAnimated:(BOOL)flag completion:(nullable void (^)(void))completion {
  48. [_viewController dismissViewControllerAnimated:flag completion:completion];
  49. }
  50. + (id<FIRAuthUIDelegate>)defaultUIDelegate {
  51. UIViewController *topViewController =
  52. [UIApplication sharedApplication].keyWindow.rootViewController;
  53. while (true){
  54. if (topViewController.presentedViewController) {
  55. topViewController = topViewController.presentedViewController;
  56. } else if ([topViewController isKindOfClass:[UINavigationController class]]) {
  57. UINavigationController *nav = (UINavigationController *)topViewController;
  58. topViewController = nav.topViewController;
  59. } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
  60. UITabBarController *tab = (UITabBarController *)topViewController;
  61. topViewController = tab.selectedViewController;
  62. } else {
  63. break;
  64. }
  65. }
  66. return [[FIRAuthDefaultUIDelegate alloc] initWithViewController:topViewController];
  67. }
  68. @end
  69. NS_ASSUME_NONNULL_END