8

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?

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
RandomGuy
  • 343
  • 3
  • 10

6 Answers6

11

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.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
5

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()
}
Alessandro Francucci
  • 1,528
  • 17
  • 25
  • 1
    This works — add this before viewDidLoad or viewWillAppear, and then call setupRefreshControl() within viewDidLoad or viewWillAppear. – zzz Oct 19 '18 at 18:22
4

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.

Alan Jay
  • 151
  • 1
  • 11
2

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.

iDoc
  • 452
  • 3
  • 8
1

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))
}
anuragtk
  • 89
  • 3
0

Try to declare this like below.

refreshControl.addTarget(self, action: #selector(yourViewController.refreshWebView), forControlEvents: UIControlEvents.ValueChanged)

Hope this works..

JAck
  • 854
  • 9
  • 18