AuthWebView.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. @objc(FIRAuthWebView) public class AuthWebView: UIView {
  21. public lazy var webView: WKWebView = createWebView()
  22. public lazy var spinner: UIActivityIndicatorView = createSpinner()
  23. override init(frame: CGRect) {
  24. super.init(frame: frame)
  25. backgroundColor = .white
  26. initializeSubviews()
  27. }
  28. @available(*, unavailable)
  29. required init?(coder aDecoder: NSCoder) {
  30. fatalError("init(coder:) has not been implemented")
  31. }
  32. private func initializeSubviews() {
  33. let webView = createWebView()
  34. let spinner = createSpinner()
  35. // The order of the following controls z-order.
  36. addSubview(webView)
  37. addSubview(spinner)
  38. layoutSubviews()
  39. self.webView = webView
  40. self.spinner = spinner
  41. }
  42. // TODO: Should not be public
  43. override public func layoutSubviews() {
  44. super.layoutSubviews()
  45. let height = bounds.size.height
  46. let width = bounds.size.width
  47. webView.frame = CGRect(x: 0, y: 0, width: width, height: height)
  48. spinner.center = webView.center
  49. }
  50. private func createWebView() -> WKWebView {
  51. let webView = WKWebView(frame: .zero)
  52. // Trickery to make the web view not do weird things (like showing a black background when
  53. // the prompt in the navigation bar animates changes.)
  54. webView.isOpaque = false
  55. webView.backgroundColor = .clear
  56. webView.scrollView.isOpaque = false
  57. webView.scrollView.backgroundColor = .clear
  58. webView.scrollView.bounces = false
  59. webView.scrollView.alwaysBounceVertical = false
  60. webView.scrollView.alwaysBounceHorizontal = false
  61. return webView
  62. }
  63. private func createSpinner() -> UIActivityIndicatorView {
  64. if #available(iOS 13.0, macCatalyst 13.0, *) {
  65. return UIActivityIndicatorView(style: .medium)
  66. } else {
  67. return UIActivityIndicatorView(style: .gray)
  68. }
  69. }
  70. }
  71. #endif