Displaying activity indicator on WKWebView using swift

房东的猫 提交于 2019-12-03 09:40:36

As commented, you forgot to set the webView delegate:

override func loadView() {
    super.loadView()
    self.webView = WKWebView()
    self.webView.navigationDelegate = self
    self.view = self.webView
}

You should use the delegate methods for all other purposes, but key path monitoring works fine for this one purpose.

Here is a Swift 4 implementation that works fine.

// Somewhere in your view controller
private var loadingObservation: NSKeyValueObservation?

private lazy var loadingIndicator: UIActivityIndicatorView = {
    let spinner = UIActivityIndicatorView()
    spinner.translatesAutoresizingMaskIntoConstraints = false
    spinner.color = .black
    return spinner
}()

override func viewDidLoad() {
    super.viewDidLoad()

    // Setup...

    loadingObservation = webView.observe(\.isLoading, options: [.new, .old]) { [weak self] (_, change) in
        guard let strongSelf = self else { return }

        // this is fine
        let new = change.newValue!
        let old = change.oldValue!

        if new && !old {
            strongSelf.view.addSubview(strongSelf.loadingIndicator)
            strongSelf.loadingIndicator.startAnimating()
            NSLayoutConstraint.activate([strongSelf.loadingIndicator.centerXAnchor.constraint(equalTo: strongSelf.view.centerXAnchor),
                                         strongSelf.loadingIndicator.centerYAnchor.constraint(equalTo: strongSelf.view.centerYAnchor)])
            strongSelf.view.bringSubview(toFront: strongSelf.loadingIndicator)
        }
        else if !new && old {
            strongSelf.loadingIndicator.stopAnimating()
            strongSelf.loadingIndicator.removeFromSuperview()
        }
    }
}

Please, below code which is working fine[Swift 4.2].

@IBOutlet weak var wv: WKWebView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!

override func viewDidLoad() {
    super.viewDidLoad()
     loadYoutube(videoID: "KqNS7uAvOxk")     
}

Now load Youtube Video

 func loadYoutube(videoID:String) {
    guard let youtubeURL = URL(string: "https://www.youtube.com/embed/\(videoID)")
        else { return }
    wv.load( URLRequest(url: youtubeURL) )
    wv.navigationDelegate = self
}

Implement below this function:

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

Implement below these three delegate method:

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

Let me know if it is not working.

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