title: Swift: How can I add an ActivtyIndicator to this WebView that shows when loading a website?
body: <p>How can add an ActivityIndicator (spinning wheel) to this WebView when it's loading a website?</p>
  <p>Here's the code for the WebView that I'm currently using:</p>
  <pre><code>import Foundation
  import SwiftUI
  import WebKit
  
  struct WebView : UIViewRepresentable {
      
      var url: String
      
      func makeUIView(context: Context) -&gt; WKWebView {
          
          guard let url = URL(string: self.url) else {
              return WKWebView()
          }
          
          let request = URLRequest(url: url)
          let wkWebView = WKWebView()
          wkWebView.load(request)
          return wkWebView
      }
  
      func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext &lt;WebView&gt;) {
      }
  }
  </code></pre>
  <p>When I want to show the WebView in some other view in my app, I use this piece of code to show the WebView and tell the WebView what URL to load:</p>
  <pre><code>WebView(url: &quot;https://www.google.com&quot;)
  </code></pre>
  <p>The URL is passed as a string which allows me to easily define a different URL every time I use the WebView. That way I can put two WebViews in a view at the same time showing different websites like this:</p>
  <pre><code>VStack {
  WebView(url: &quot;https://www.apple.com&quot;)
  WebView(url: &quot;https://www.google.com&quot;)
  }
  </code></pre>
  <p>I have already tried to use this code to implement an ActivityMonitor:</p>
  <pre><code>import WebKit
  import SwiftUI
  
  struct Webview: UIViewRepresentable {
  let url: URL
  var activityIndicator: UIActivityIndicatorView! = UIActivityIndicatorView(frame: CGRect(x: (UIScreen.main.bounds.width / 2) - 30, y: (UIScreen.main.bounds.height / 2) - 30, width: 60, height: 60))
  
  func makeUIView(context: Context) -&gt; UIView {
      let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
      let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
      webview.navigationDelegate = context.coordinator
      
      let request = URLRequest(url: self.url, cachePolicy: .returnCacheDataElseLoad)
      webview.load(request)
      
      view.addSubview(webview)
      activityIndicator.backgroundColor = UIColor.gray
      activityIndicator.startAnimating()
      activityIndicator.color = UIColor.white
      activityIndicator.layer.cornerRadius = 8
      activityIndicator.clipsToBounds = true
      
      view.addSubview(activityIndicator)
      return view
  }
  
  func updateUIView(_ webview: UIView, context: UIViewRepresentableContext&lt;Webview&gt;) {
  }
  
  func makeCoordinator() -&gt; WebViewHelper {
      WebViewHelper(self)
  }
  }
  
  class WebViewHelper: NSObject, WKNavigationDelegate {
  
  var parent: Webview
  init(_ parent: Webview) {
      self.parent = parent
      super.init()
  }
  func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
      parent.activityIndicator.isHidden = true
  }
  
  func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
      parent.activityIndicator.isHidden = true
      print(&quot;error: \(error)&quot;)
  }
  
  func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
      parent.activityIndicator.isHidden = true
      print(&quot;error \(error)&quot;)
  }
  }
  </code></pre>
  <p>But that didn't work since it wouldn't let me pass the URL as a string.</p>
  <p>Thanks for the help!</p>