Search code examples
swiftxcodeuirefreshcontrol

Swift: Custom action for UIRefreshControl in WKWebView


I'm using the newest Xcode and Swift version.

I'm using WKWebView to display content from my webserver.

I'm using the following code to pull to refresh:

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshWebView(_:)), for: UIControl.Event.valueChanged)
homeWebView.scrollView.addSubview(refreshControl)

Since refreshing seems not to be the same as (re)loading an URL, this results to some unexpected behavior. Also, it doesn't trigger func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {.

Is there any way to catch the pull to refresh function and make some custom stuff e.g. loading another URL?


Solution

  • You could just change the action in Selector that you're passing to your custom action, here's how:

    refreshControl.addTarget(self, action: #selector(myCustomAction), for: UIControl.Event.valueChanged)
    

    Action:

    @objc func myCustomAction() {
        print("my custom action here...")
    }