AuthWebView.swift 2.4 KB

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