I\'ve implemented a straight forward WKWebView
in iOS.
var refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: Sel
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.
Try to declare this like below.
refreshControl.addTarget(self, action: #selector(yourViewController.refreshWebView), forControlEvents: UIControlEvents.ValueChanged)
Hope this works..
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()
}
Just to say looking at this in Swift 3 and the answer from JAck helped me solve this issue:
in loadView()/viewDidLoad()
webView.scrollView.bounces = true
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
webView.scrollView.addSubview(refreshControl)
and then a separate function
func refreshWebView(sender: UIRefreshControl) {
print("refersh")
//
sender.endRefreshing()
}
And I'm now seeing the refresh being printed.
You have to enable bouncing for the scrollview of the webview:
webView.scrollView.bounces = true
This will let you drag the webview down which will trigger your UIRefreshControl.
Following code will put the refresh control in scrollview of webview.Initially it will load google.com. To see pull to refresh clearly I have set white color to the background of scroll view so it is clearly visible and on pull to refresh webview opens facebook page.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.refreshControl = UIRefreshControl.init()
refreshControl!.addTarget(self, action:#selector(refreshControlClicked), for: UIControlEvents.valueChanged)
self.webView.scrollView.addSubview(self.refreshControl!)
self.webView.scrollView.backgroundColor = UIColor.white
self.webView.delegate = self
let url:URL = URL.init(string:"https://www.google.com")!
self.loadRequestWithUrl(URLRequest.init(url: url))
}
func webViewDidStartLoad(_ webView: UIWebView) {
NSLog("website loaded")
}
func loadRequestWithUrl(_ urlRequest : URLRequest?){
if(urlRequest != nil){
self.webView.loadRequest(urlRequest!)
}
}
func refreshControlClicked(){
let url:URL = URL.init(string:"https://www.facebook.com")!
self.loadRequestWithUrl(URLRequest.init(url: url))
}