Search code examples
wkwebviewswiftuiios13

How to access goBack and goForward via UIViewRepresentable


I have added a WKwebview in SwiftUI via UIVewRepresentable. I am having difficulties getting some buttons to make the web view go back and go forward. Below is the two classes I have made but I am not even getting print to work ( maybe a bug? ).

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)
}

func goBack(){
    // go back
    print("go back")
}

func goForward (){
    // go forward
    print("go forward")
}
}



import SwiftUI

struct FullWebView : View {

let webview = Webview(request: URLRequest(url: URL(string: "https://www.apple.com")!))

var body: some View {
    VStack{
        HStack{
            Button(action: {
                //do something
                self.webview.goBack()
            }){
                Text("back")
            }

            Spacer()

            Button(action: {
                //do something
                self.webview.goForward()
            }){
                Text("forward")
            }

        }

        webview
    }
}
}

Solution

  • dfd thank you for pointing me on the correct path, your answer needed a little modification because adding the webview as a var to the struct means I needed to set a value on it when I use it, so I also had to add a custom init method. Anyway here is the code that works.

    Webview struct

    import SwiftUI
    
    import WebKit
    
    struct Webview : UIViewRepresentable {
    
    
        let request: URLRequest
        var webview: WKWebView?
    
        init(web: WKWebView?, req: URLRequest) {
            self.webview = WKWebView()
            self.request = req
        }
    
        func makeUIView(context: Context) -> WKWebView  {
            return webview!
        }
    
        func updateUIView(_ uiView: WKWebView, context: Context) {
            uiView.load(request)
        }
    
        func goBack(){
            webview?.goBack()
        }
    
        func goForward(){
            webview?.goForward()
        }
      }
    

    and here is the struct using the webview and adding the buttons to the top

    import SwiftUI
    
    struct FullWebView : View {
    
    
    let webview = Webview(web: nil, req: URLRequest(url: URL(string: "https://www.apple.com")!))
    
    var body: some View {
        VStack{
            HStack{
                Button(action: {
                    //do something
                    self.webview.goBack()
                }){
                    Text("back")
                }
    
                Spacer()
    
                Button(action: {
                    //do something
                    self.webview.goForward()
                }){
                    Text("forward")
                }
    
            }
    
            webview
        }
    }
    }