5 Replies
      Latest reply on Jan 16, 2020 2:13 AM by esaruoho
      Laurens724 Level 1 Level 1 (0 points)

        I just downloaded the new Xcode Beta and I like SwiftUI.

        But how do I implement a WebView (WebKit) in SwiftUI?

        • Re: How do I implement a WebView (WebKit) in SwiftUI?
          Laurens724 Level 1 Level 1 (0 points)

          ive tried to import webkit but i do not find webkit view in the elements...

            • Re: How do I implement a WebView (WebKit) in SwiftUI?
              Bradley Hilton Level 1 Level 1 (0 points)

              You can wrap views with `UIViewRepresentable`:


              //
              //  WebView.swift
              //  Todos
              //
              //  Created by Bradley Hilton on 6/5/19.
              //  Copyright © 2019 Brad Hilton. All rights reserved.
              //
              
              import SwiftUI
              import WebKit
              
              struct WebView : UIViewRepresentable {
                  
                  let request: URLRequest
                  
                  func makeUIView(context: Context) -> WKWebView  {
                      return WKWebView()
                  }
                  
                  func updateUIView(_ uiView: WKWebView, context: Context) {
                      uiView.load(request)
                  }
                  
              }
              
              #if DEBUG
              struct WebView_Previews : PreviewProvider {
                  static var previews: some View {
                      WebView(request: URLRequest(url: URL(string: "https://www.apple.com")!))
                  }
              }
              #endif
              
              
                • Re: How do I implement a WebView (WebKit) in SwiftUI?
                  OxyFlax Level 1 Level 1 (0 points)

                  Thanks this is good, but when I open a new WebView, the last loaded site remains until new url is loaded. Any clue to make it disappear?

                  • Re: How do I implement a WebView (WebKit) in SwiftUI?
                    bzouchir Level 1 Level 1 (0 points)

                    How do you implement delegates?

                    we used to use extensions on the ViewController:

                     

                    extension WebViewController: WKNavigationDelegate {
                    
                        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
                            track("didFinish")
                            self.hideProgressView()
                        }
                        
                        func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
                            track("webViewWebContentProcessDidTerminate")
                        }
                        
                        func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
                            track("didFail")
                            self.hideProgressView()
                        }
                    }
                    
                    • Re: How do I implement a WebView (WebKit) in SwiftUI?
                      esaruoho Level 1 Level 1 (0 points)

                      hi, your code said

                      "Use of undeclared type 'UIViewRepresentable'"

                      "Use of undeclared type 'Context'"

                      "Use of undeclared type 'Context'"

                      "Return type of static property 'previews' requires that 'WebView' conform to 'View'".

                       

                      I'm very confused. I don't know how to proceed? Any ideas?