Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save apple-avadhesh/12200dff778717e3beb5b268cdd338f7 to your computer and use it in GitHub Desktop.
Save apple-avadhesh/12200dff778717e3beb5b268cdd338f7 to your computer and use it in GitHub Desktop.
WKWebView with UIActivityIndicatorView
import Foundation
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {
var loader: UIActivityIndicatorView!
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self
webView.navigationDelegate = self
loader = UIActivityIndicatorView()
loader.center = self.view.center
loader.hidesWhenStopped = true
loader.style = UIActivityIndicatorView.Style.large
self.view.addSubview(loader)
let myURL = URL(string:"https://www.sepettte.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("didFinish")
showLoader(status: false)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("didStartProvisionalNavigation")
showLoader(status: true)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
print("didFail")
showLoader(status: false)
}
func showLoader(status: Bool) {
if status {
loader.startAnimating()
} else {
loader.stopAnimating()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment