Pull to refresh not working in iOS WebView

我们两清 提交于 2019-11-30 20:21:26

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()
}

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))
}

Try to declare this like below.

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

Hope this works..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!