Nessuna descrizione

FIRAuthDefaultUIDelegate.m 3.2KB

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