I've implemented a straight forward WKWebView
in iOS.
var refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: Selector("refreshWebView"), forControlEvents: UIControlEvents.ValueChanged)
This is in viewDidLoad()
And the corresponding refreshWebView
function. But when I run it on mobile or simulator, no matter how much I drag, there is no pull to refresh action. Nothing is triggered, and it almost feels like I am stuck to the upper bounds of the screen. Where can be going wrong?
Try to change your code like this, If you want to pull to refresh
the webView
you need to addSubView
UIRefreshControl
object in the ScrollView
of webView
like this
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(self.refreshWebView(_:)), forControlEvents: UIControlEvents.ValueChanged)
webView.scrollView.addSubview(refreshControl)
Add func like this
func refreshWebView(sender: UIRefreshControl) {
print("refersh")
webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://google.com")!))
sender.endRefreshing()
}
Hope this will help you.
SWIFT 4 Solution (Based on Nirav D answer)
You can simply call this setup method in your viewDidLoad
:
private func setupRefreshControl() {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshWebView(sender:)), for: UIControlEvents.valueChanged)
webView.scrollView.addSubview(refreshControl)
}
@objc
private func refreshWebView(sender: UIRefreshControl) {
print("refreshing...")
webView.load(URLRequest(url: url))
sender.endRefreshing()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With