1

I have an iOS app build with Xcode and Swift.

When I pull to refresh the activityIndicator spins. When I release the animation/spin will stop immediately but the website only refreshes/reloads after ca. 3/4-1 seconds.

Is there a way to show the animation/spin wheel till the end of refreshing? This is my code:

    func pullToRefresh() {
        self.homewebview.reload()
        self.refreshControl.endRefreshing()
//        NSURLCache.sharedURLCache().removeAllCachedResponses()
    }
    func addRefreshControl() {
        refreshControl = UIRefreshControl()
        refreshControl.attributedTitle = NSAttributedString(string: "Einen Moment bitte…")
        refreshControl.addTarget(self, action: #selector(FirstViewController.pullToRefresh), forControlEvents:.ValueChanged)
        self.homewebview.scrollView.addSubview(refreshControl)
        refreshControl.tintColor = UIColor.blackColor()
    }
syntonym
  • 7,134
  • 2
  • 32
  • 45

1 Answers1

0

You need to call the endRefreshing method of UIRefreshControl in delegate method of UIWebViewDelegate like this

override func viewDidLoad() {
    super.viewDidLoad()
    self.webView.delegate = self
}

func webViewDidFinishLoad(webView: UIWebView) {
    self.refreshControl.endRefreshing()
}

Also remove self.refreshControl.endRefreshing() from pullToRefresh method.

Hope this will help you.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Hi, thanks for trying to help me. The animation and spinning wheel don't disappear now. The wheel spins all time. –  Jul 08 '16 at 08:45
  • You need to set the delegate of your `webView` object to `self`. Check my edited answer. – Nirav D Jul 08 '16 at 08:47
  • For `self.webView.delegate = self` I get: Cannot assign value of type 'MyViewController' to type 'UIWebViewDelegate?' –  Jul 08 '16 at 08:54
  • I have to do it like this, right? Code: `class MyViewController: UIViewController, UIWebViewDelegate {` –  Jul 08 '16 at 08:56
  • Yes also add this `webViewDidFinishLoad` delegate method of `UIWebViewDelegate` in your `MyViewController` like i did in answer. – Nirav D Jul 08 '16 at 08:58
  • @David Welcome, Happy Coding :) – Nirav D Jul 08 '16 at 09:08
  • Small question: When adding `NSURLCache.sharedURLCache().removeAllCachedResponses()` to `pullToRefresh` all cache will deleted when refreshing, right? –  Jul 08 '16 at 09:16
  • Read this [answer](http://stackoverflow.com/a/5606703/6433023) it will give you whole idea about `NSURLCache`. – Nirav D Jul 08 '16 at 09:22