AuthWebView.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. class AuthWebView: UIView {
  22. lazy var webView: WKWebView = createWebView()
  23. 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. override 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. return UIActivityIndicatorView(style: .medium)
  65. }
  66. }
  67. #endif