Pull the update to not work in iOS WebView

I implemented direct WKWebView on 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 a mobile or simulator, no matter how much I drag, there is no effect to update the action. Nothing works, and it almost seems like I'm sticking to the top of the screen. Where could it be wrong?

+6
source share
5 answers

Try changing your code as follows. If you want to pull on a refresh webView , you need to addSubView UIRefreshControl object in a 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

 func refreshWebView(sender: UIRefreshControl) { print("refersh") webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://google.com")!)) sender.endRefreshing() } 

Hope this helps you.

+9
source

You need to enable the scan to scroll in the webview:

 webView.scrollView.bounces = true 

This will allow you to drag the web view down, which will call your UIRefreshControl.

+2
source

Just saying looking at it in Swift 3 and the answer from JAck helped me solve this problem:

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 now I see that the update has been printed.

+2
source

Try declaring it as shown below.

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

Hope this works.

0
source

The following code will place the update control in a scrollview webview. He will download google.com first. To clearly see what needs to be updated, I set the white color on the scroll background so that it is clearly visible, and when I click to refresh the web view, the facebook page opens.

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