FIRAuthWebView.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_IOS
  18. #import "FirebaseAuth/Sources/Utilities/FIRAuthWebView.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. @implementation FIRAuthWebView
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. self.backgroundColor = [UIColor whiteColor];
  25. [self initializeSubviews];
  26. }
  27. return self;
  28. }
  29. /** @fn initializeSubviews
  30. @brief Initializes the subviews of this view.
  31. */
  32. - (void)initializeSubviews {
  33. WKWebView *webView = [self createWebView];
  34. UIActivityIndicatorView *spinner = [self createSpinner];
  35. // The order of the following controls z-order.
  36. [self addSubview:webView];
  37. [self addSubview:spinner];
  38. [self layoutSubviews];
  39. _webView = webView;
  40. _spinner = spinner;
  41. }
  42. - (void)layoutSubviews {
  43. CGFloat height = self.bounds.size.height;
  44. CGFloat width = self.bounds.size.width;
  45. _webView.frame = CGRectMake(0, 0, width, height);
  46. _spinner.center = _webView.center;
  47. }
  48. /** @fn createWebView
  49. @brief Creates a web view to be used by this view.
  50. @return The newly created web view.
  51. */
  52. - (WKWebView *)createWebView {
  53. WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero];
  54. // Trickery to make the web view not do weird things (like showing a black background when
  55. // the prompt in the navigation bar animates changes.)
  56. webView.opaque = NO;
  57. webView.backgroundColor = [UIColor clearColor];
  58. webView.scrollView.opaque = NO;
  59. webView.scrollView.backgroundColor = [UIColor clearColor];
  60. webView.scrollView.bounces = NO;
  61. webView.scrollView.alwaysBounceVertical = NO;
  62. webView.scrollView.alwaysBounceHorizontal = NO;
  63. return webView;
  64. }
  65. /** @fn createSpinner
  66. @brief Creates a spinner to be used by this view.
  67. @return The newly created spinner.
  68. */
  69. - (UIActivityIndicatorView *)createSpinner {
  70. UIActivityIndicatorViewStyle spinnerStyle;
  71. #if defined(TARGET_OS_MACCATALYST)
  72. if (@available(iOS 13.0, *)) {
  73. spinnerStyle = UIActivityIndicatorViewStyleMedium;
  74. } else {
  75. // iOS 13 deprecation
  76. #pragma clang diagnostic push
  77. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  78. spinnerStyle = UIActivityIndicatorViewStyleGray;
  79. #pragma clang diagnostic pop
  80. }
  81. #else
  82. spinnerStyle = UIActivityIndicatorViewStyleGray;
  83. #endif
  84. UIActivityIndicatorView *spinner =
  85. [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:spinnerStyle];
  86. return spinner;
  87. }
  88. @end
  89. NS_ASSUME_NONNULL_END
  90. #endif