AuthWebView.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if os(iOS)
  15. import UIKit
  16. import WebKit
  17. /** @class AuthWebView
  18. @brief A class responsible for creating a WKWebView for use within Firebase Auth.
  19. */
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. @objc(FIRAuthWebView) public class AuthWebView: UIView {
  22. public lazy var webView: WKWebView = createWebView()
  23. public lazy var spinner: UIActivityIndicatorView = createSpinner()
  24. override init(frame: CGRect) {
  25. super.init(frame: frame)
  26. backgroundColor = .white
  27. initializeSubviews()
  28. }
  29. @available(*, unavailable)
  30. required init?(coder aDecoder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. private func initializeSubviews() {
  34. let webView = createWebView()
  35. let spinner = createSpinner()
  36. // The order of the following controls z-order.
  37. addSubview(webView)
  38. addSubview(spinner)
  39. layoutSubviews()
  40. self.webView = webView
  41. self.spinner = spinner
  42. }
  43. // TODO: Should not be public
  44. override public func layoutSubviews() {
  45. super.layoutSubviews()
  46. let height = bounds.size.height
  47. let width = bounds.size.width
  48. webView.frame = CGRect(x: 0, y: 0, width: width, height: height)
  49. spinner.center = webView.center
  50. }
  51. private func createWebView() -> WKWebView {
  52. let webView = WKWebView(frame: .zero)
  53. // Trickery to make the web view not do weird things (like showing a black background when
  54. // the prompt in the navigation bar animates changes.)
  55. webView.isOpaque = false
  56. webView.backgroundColor = .clear
  57. webView.scrollView.isOpaque = false
  58. webView.scrollView.backgroundColor = .clear
  59. webView.scrollView.bounces = false
  60. webView.scrollView.alwaysBounceVertical = false
  61. webView.scrollView.alwaysBounceHorizontal = false
  62. return webView
  63. }
  64. private func createSpinner() -> UIActivityIndicatorView {
  65. return UIActivityIndicatorView(style: .medium)
  66. }
  67. }
  68. #endif