Home > Software engineering >  Pull to refresh for WKWebView
Pull to refresh for WKWebView

Time:04-14

I'm trying to add pull to refresh on my wkwebview app. I'm using UIViewRepresentable for my webview, so I don't have the onViewLoad function and view controllers. Here's my code:

var wbWebViewEl: WKWebView = WKWebView()
struct SwiftUiWebView: UIViewRepresentable {
    ....
    class refreshWebViewClass {
    @objc func refreshWebView1(sender: UIRefreshControl) {
        print("test debug :D")
        wbWebViewEl.reload()
        DispatchQueue.main.async {
        sender.endRefreshing()
        }
    }
    func makeUIView(context: Context) -> WKWebView {    
        ......
        wbWebViewEl.scrollView.bounces = true
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(refreshWebViewClass.refreshWebView1), for: UIControl.Event.valueChanged)
        wbWebViewEl.scrollView.addSubview(refreshControl)
        wbWebViewEl.scrollView.refreshControl = refreshControl
        .....
    }

    .....

}

At the moment, when you swipe down, it shows the refresh loading spinner, but it doesn't stop spinning. Also the test print I put in the refreshWebView1 function, doesn't show in the logcat. Does anyone know what I'm getting wrong here? If you need to see anymore code let me know :)

CodePudding user response:

Looks like your refreshWebViewClass should be a Coordinator. Then, in your addTarget, the target would be context.coordinator, not self, which doesn't make sense here, since self doesn't actually implement this method.

  • Related