user8794794
user8794794

Reputation:

How to add UIActivityIndicatorView to WKWebView?

I'm new to Swift and I need some help implementing the UIActivityInidicatorView to WKWebView. It should appear while the webpage is loading and disappear when the website finished loading.

Here is my code so far:

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let myURL = URL(string: "https://www.google.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}

Upvotes: 1

Views: 2452

Answers (3)

Ian Pinto
Ian Pinto

Reputation: 2299

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

    var webView: WKWebView!
    var activityIndicator: UIActivityIndicatorView!

    override func viewDidLoad() {
        webView = WKWebView(frame: CGRect.zero)
        webView.navigationDelegate = self
        webView.uiDelegate = self

        view.addSubview(webView)

        activityIndicator = UIActivityIndicatorView()
        activityIndicator.center = self.view.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray

        view.addSubview(activityIndicator)

        webView.load(URLRequest(url: URL(string: "http://google.com")!))
    }

    func showActivityIndicator(show: Bool) {
        if show {
            activityIndicator.startAnimating()
        } else {
            activityIndicator.stopAnimating()
        }
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        showActivityIndicator(show: false)
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        showActivityIndicator(show: true)
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        showActivityIndicator(show: false)
    }
}

Upvotes: 2

Zee
Zee

Reputation: 327

Try this i hope this will help you

import UIKit
import WebKit

 class ViewController: UIViewController, WKNavigationDelegate  {

  @IBOutlet weak var webView: WKWebView!
  @IBOutlet weak var ActivityIndicator: UIActivityIndicatorView!

  override func viewDidLoad() {
    super.viewDidLoad()
    let myURL = URL(string: "https://www.google.com")
    let myRequest = URLRequest(url: myURL!)

    webView.navigationDelegate = self

    webView.load(myRequest)

   }

   func webView(_ webView: WKWebView,didStart navigation: WKNavigation!) {
      print("Start Page Loading")
     ActivityIndicator.statAnimating()
   }

   func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) {
      print("Page loaded")
     ActivityIndicator.stopAnimating()
   }
}

Upvotes: 0

user8794794
user8794794

Reputation:

I finally got it working like this:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

    @IBOutlet weak var webView: WKWebView!

    @IBOutlet weak var ActivityIndicator: UIActivityIndicatorView!

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        ActivityIndicator.stopAnimating()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let myURL = URL(string: "https://www.google.com/")
        let myRequest = URLRequest(url: myURL!)
        webView.navigationDelegate = self
        webView.load(myRequest)
    }
}

Upvotes: 0

Related Questions