title: SwiftUI - How to evaluate javascript in webview if network is down?
body: <p>I have the following webview implementation built by borrowing code (I am learning SwiftUI). I want to show a message in the webview when the networks is down. It works with a static html loaded from a string and a button but I am unable to do the same calling a web server and triggering javascript using the network status.</p>
  <pre><code>import SwiftUI
  import WebKit
  import Combine
  
  private let urlString: String = &quot;http://127.0.0.1&quot;
  
  
  class WebViewData: ObservableObject {
      @Published var parsedText: NSAttributedString? = nil
  
      var functionCaller = PassthroughSubject&lt;Void,Never&gt;()
      
      var isInit = false
      var shouldUpdateView = true
  }
  
  
  struct WebView: UIViewRepresentable {
      @StateObject var data: WebViewData
      
      func makeUIView(context: Context) -&gt; WKWebView {
          let wkWebview = WKWebView()
          wkWebview.navigationDelegate = context.coordinator
          return wkWebview
      }
      
      func updateUIView(_ uiView: WKWebView, context: Context) {
          guard data.shouldUpdateView else {
              data.shouldUpdateView = false
              return
          }
  
          context.coordinator.tieFunctionCaller(data: data)
          context.coordinator.webView = uiView
          
          guard let url = URL(string: urlString) else { return }
          let request = URLRequest(url: url)
          uiView.load(request)
  
      }
      
      func makeCoordinator() -&gt; WebViewCoordinator {
          return WebViewCoordinator(view: self)
      }
      
      func viewWillAppear(_ uiView: WKWebView, context: UIViewRepresentableContext&lt;WebView&gt;){
          
      }
  }
  
  class WebViewCoordinator : NSObject, WKNavigationDelegate {
      var parent: WebView
      var webView: WKWebView? = nil
      
      private var cancellable : AnyCancellable?
      
      init(view: WebView) {
              self.parent = view
              super.init()
          }
      
      func tieFunctionCaller(data: WebViewData) {        
          cancellable = data.functionCaller.sink(receiveValue: { _ in
              self.webView?.evaluateJavaScript(&quot;message_generator(\&quot;network_down\&quot;&quot;)
          })
      }
      
      func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
          DispatchQueue.main.async {
              if !self.parent.data.isInit {
                  self.parent.data.isInit = true
              }
          }
      }
  }
  
  </code></pre>
  <p>Here the ContentView:</p>
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      @StateObject var webViewData = WebViewData()
      @ObservedObject var networkManager = NetworkManager()
      
      var body: some View {
          ZStack{
              Color.white
              VStack {
                  ExecuteCode {
                      print(networkManager.isConnected)
                      webViewData.functionCaller.send() //???
                  }
                  WebView(data: webViewData)
                  if (!networkManager.isConnected) {
                      //???
                  }
              }
              .onReceive(webViewData.$parsedText, perform: { parsedText in
                  if let parsedText = parsedText {
                      print(parsedText)
                  }
              })
          }
      }
  }
  
  
  struct ExecuteCode : View {
      init( _ codeToExec: () -&gt; () ) {
          codeToExec()
      }
      
      var body: some View {
          return EmptyView()
      }
  }
  
  </code></pre>
  <p>Here the NetworkManager:</p>
  <pre><code>import Foundation
  import Network
  
  class NetworkManager: ObservableObject {
      let monitor = NWPathMonitor()
      let queue = DispatchQueue(label: &quot;NetworkManager&quot;)
      @Published var isConnected = true
      
      var imageName: String {
          return isConnected ? &quot;wifi&quot; : &quot;wifi.slash&quot;
      }
      
      var connectionDescription: String {
          if isConnected {
              return &quot;Internet connection looks good!&quot;
          } else {
              return &quot;It looks like you're not connected to the internet. Make sure WiFi is enabled and try again&quot;
          }
      }
      
      init() {
          monitor.pathUpdateHandler = { path in
              DispatchQueue.main.async {
                  if path.status == .satisfied {
                      self.isConnected = true
                  } else {
                      self.isConnected = false
                  }
              }
          }
          monitor.start(queue: queue)
      }
  }
  
  </code></pre>
  <p>Thanks for helping.
  Pointers to good docs are very appreciated.</p>